zoukankan      html  css  js  c++  java
  • 第7月第26天 iOS httpserver

    1. cocoahttpserver

    1)httpserver 

    [httpServer start:&error]

     

    2)HTTPConnection

     

    [newConnection start]

     

    3)HTTPMessage HTTPResponse

     

    - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData*)data withTag:(long)tag

    {

    ...

     

    // Respond properly to HTTP 'GET' and 'HEAD' commands

    httpResponse = [self httpResponseForMethod:method URI:uri];

     

    if (httpResponse == nil)

    {

    [self handleResourceNotFound];

    return;

    }

     

    [self sendResponseHeadersAndBody];

     

    https://github.com/aldrinmartoq/brayatan/

    https://github.com/robbiehanson/CocoaHTTPServer

    2.poco

    void TCPServerDispatcher::run()
    {
        AutoPtr<TCPServerDispatcher> guard(this, true); // ensure object stays alive
    
        int idleTime = (int) _pParams->getThreadIdleTime().totalMilliseconds();
    
        for (;;)
        {
            AutoPtr<Notification> pNf = _queue.waitDequeueNotification(idleTime);
            if (pNf)
            {
                TCPConnectionNotification* pCNf = dynamic_cast<TCPConnectionNotification*>(pNf.get());
                if (pCNf)
                {
                    std::auto_ptr<TCPServerConnection> pConnection(_pConnectionFactory->createConnection(pCNf->socket()));
                    poco_check_ptr(pConnection.get());
                    beginConnection();
                    pConnection->start();
                    endConnection();
                }
            }
    
            FastMutex::ScopedLock lock(_mutex);
            if (_stopped || (_currentThreads > 1 && _queue.empty()))
            {
                --_currentThreads;
                break;
            }
        }
    }
    void HTTPServerConnection::run()
    {
        std::string server = _pParams->getSoftwareVersion();
        HTTPServerSession session(socket(), _pParams);
        while (!_stopped && session.hasMoreRequests())
        {
            try
            {
                Poco::FastMutex::ScopedLock lock(_mutex);
    
                if (!_stopped)
                {
                    HTTPServerResponseImpl response(session);
                    HTTPServerRequestImpl request(response, session, _pParams);
    
                    Poco::Timestamp now;
                    response.setDate(now);
                    response.setVersion(request.getVersion());
                    response.setKeepAlive(_pParams->getKeepAlive() && request.getKeepAlive() && session.canKeepAlive());
                    if (!server.empty())
                        response.set("Server", server);
                    try
                    {
                        std::auto_ptr<HTTPRequestHandler> pHandler(_pFactory->createRequestHandler(request));
                        if (pHandler.get())
                        {
                            if (request.expectContinue())
                                response.sendContinue();
    
                            pHandler->handleRequest(request, response);
                            session.setKeepAlive(_pParams->getKeepAlive() && response.getKeepAlive() && session.canKeepAlive());
                        }
                        else sendErrorResponse(session, HTTPResponse::HTTP_NOT_IMPLEMENTED);
                    }
                    catch (Poco::Exception&)
                    {
                        if (!response.sent())
                        {
                            try
                            {
                                sendErrorResponse(session, HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
                            }
                            catch (...)
                            {
                            }
                        }
                        throw;
                    }
                }
            }
            catch (NoMessageException&)
            {
                break;
            }
            catch (MessageException&)
            {
                sendErrorResponse(session, HTTPResponse::HTTP_BAD_REQUEST);
            }
        }
    }
    class TimeRequestHandler: public HTTPRequestHandler
        /// Return a HTML document with the current date and time.
    {
    public:
        TimeRequestHandler(const std::string& format):
            _format(format)
        {
        }
    
        void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
        {
            Application& app = Application::instance();
            app.logger().information("Request from " + request.clientAddress().toString());
    
            Timestamp now;
            std::string dt(DateTimeFormatter::format(now, _format));
    
            response.setChunkedTransferEncoding(true);
            response.setContentType("text/html");
    
            std::ostream& ostr = response.send();
            ostr << "<html><head><title>HTTPTimeServer powered by POCO C++ Libraries</title>";
            ostr << "<meta http-equiv="refresh" content="1"></head>";
            ostr << "<body><p style="text-align: center; font-size: 48px;">";
            ostr << dt;
            ostr << "</p></body></html>";
        }
    
    private:
        std::string _format;
    };
  • 相关阅读:
    Python 正则表达式(分组)
    django 笔记
    Java代理和动态代理机制分析和应用
    Chrome浏览器如何调试移动端网页信息
    【数据分析】Excle中安装数据分析工具
    【BigData】Java基础_socket编程中使用多线程
    【BigData】Java基础_多线程
    【BigData】Java基础_socket编程
    财务报表之利润表
    资产负债表的会计恒等式
  • 原文地址:https://www.cnblogs.com/javastart/p/6769908.html
Copyright © 2011-2022 走看看