首页 > 代码库 > 第7月第26天 iOS httpserver
第7月第26天 iOS httpserver
1. cocoahttpserver
1)httpserver
<style></style>[httpServer start:&error]
2)HTTPConnection
<style></style>
[newConnection start]
3)HTTPMessage HTTPResponse
<style></style>
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData*)data withTag:(long)tag
{
...
<style></style><style></style><style></style>
// 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;};
<style></style><style></style><style></style>
第7月第26天 iOS httpserver
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。