第7月第26天 iOS httpserver
Posted lianhuaren
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第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; };
!-- p.p1>!-- p.p1>!-- p.p1>!-- p.p1>!-- p.p1>!-- p.p1>!-- p.p1>!-- p.p1>!-- p.p1>
以上是关于第7月第26天 iOS httpserver的主要内容,如果未能解决你的问题,请参考以下文章