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;
    };
  • 相关阅读:
    判断activity是否显示在界面上
    限制EditText的输入字数
    安卓自定义类似TabHost的导航栏
    安卓中加载布局文件的三种方法
    绘图——Android绘图基础:Canvas、Paint等
    使用简单图片
    使用原始资源
    MySQL分表(Partition)学习研究报告
    Docker基础知识介绍
    Python开发系列
  • 原文地址:https://www.cnblogs.com/javastart/p/6769908.html
Copyright © 2011-2022 走看看