7th Month 26th Day IOS httpserver

Source: Internet
Author: User

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

voidTcpserverdispatcher::run () {autoptr<TCPServerDispatcher> Guard ( This,true);//ensure object stays alive    intIdleTime = (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::scopedlockLock(_mutex); if(_stopped | | (_currentthreads >1&&_queue.empty ())) {            --_currentthreads;  Break; }    }}

voidHttpserverconnection::run () {std::stringServer = _pparams->getsoftwareversion ();    Httpserversession session (socket (), _pparams);  while(!_stopped &&session.hasmorerequests ()) {        Try{Poco::fastmutex::scopedlockLock(_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 ()); }                    ElseSenderrorresponse (Session, httpresponse::http_not_implemented); }                Catch(poco::exception&)                {                    if(!response.sent ()) {                        Try{Senderrorresponse (session, Httpresponse::http_internal_server_err                        OR); }                        Catch (...) {                        }                    }                    Throw; }            }        }        Catch(nomessageexception&)        {             Break; }        Catch(messageexception&) {Senderrorresponse (session, Httpresponse::http_bad_request); }    }}

classTimerequesthandler: PublicHttprequesthandler///Return a HTML document with the current date and time.{ Public: Timerequesthandler (ConstSTD::string&format): _format (format) {}voidHandleRequest (httpserverrequest& request, httpserverresponse&response) {Application& app =application::instance (); App.logger (). Information ("Request from"+request.clientaddress (). toString ());        Timestamp now; STD::stringDT (Datetimeformatter::format (now, _format)); Response.setchunkedtransferencoding (true); Response.setcontenttype ("text/html"); Std::ostream& ostr =Response.send (); Ostr<<""; Ostr<<"<meta http-equiv=\ "refresh\" content=\ "1\" >"; Ostr<<"<body><p style=\ "Text-align:center font-size:48px;\" >"; Ostr<<DT; Ostr<<"</p></body>"; }Private: std::string_format;};

7th Month 26th Day IOS httpserver

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.