![]() |
RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* chunked_http_endpoint.cc 00002 Jeremy Barnes, 7 March 2012 00003 Copyright (c) 2012 Datacratic. All rights reserved. 00004 00005 Endpoint for chunked http requests. 00006 */ 00007 00008 #include "soa/service/chunked_http_endpoint.h" 00009 #include <boost/make_shared.hpp> 00010 00011 00012 using namespace std; 00013 using namespace ML; 00014 00015 00016 namespace Datacratic { 00017 00018 00019 /*****************************************************************************/ 00020 /* CHUNKED HTTP HANDLER */ 00021 /*****************************************************************************/ 00022 00023 long ChunkedHttpHandler::created = 0; 00024 long ChunkedHttpHandler::destroyed = 0; 00025 00026 ChunkedHttpHandler:: 00027 ChunkedHttpHandler() 00028 { 00029 atomic_add(created, 1); 00030 } 00031 00032 ChunkedHttpHandler:: 00033 ~ChunkedHttpHandler() 00034 { 00035 atomic_add(destroyed, 1); 00036 } 00037 00038 void 00039 ChunkedHttpHandler:: 00040 onGotTransport() 00041 { 00042 this->endpoint = dynamic_cast<ChunkedHttpEndpoint *>(get_endpoint()); 00043 00044 if (!this->endpoint) 00045 throw Exception("ChunkedHttpHandler needs to be owned by an " 00046 "ChunkedHttpEndpoint"); 00047 00048 HttpConnectionHandler::onGotTransport(); 00049 00050 startReading(); 00051 } 00052 00053 void 00054 ChunkedHttpHandler:: 00055 handleDisconnect() 00056 { 00057 doEvent("disconnection"); 00058 closeWhenHandlerFinished(); 00059 } 00060 00061 void 00062 ChunkedHttpHandler:: 00063 handleHttpChunk(const HttpHeader & header, 00064 const std::string & chunkHeader, 00065 const std::string & chunk) 00066 { 00067 endpoint->onChunk(header, chunkHeader, chunk); 00068 } 00069 00070 void 00071 ChunkedHttpHandler:: 00072 handleError(const std::string & message) 00073 { 00074 doEvent("error"); 00075 00076 send(ML::format("HTTP/1.1 400 Bad Request\r\n" 00077 "Content-Type: text/plain\r\n" 00078 "Content-Length: %zd\r\n" 00079 "Connection: Close\r\n" 00080 "\r\n" 00081 "%s", message.size(), message.c_str()), 00082 NEXT_CLOSE); 00083 } 00084 00085 std::string 00086 ChunkedHttpHandler:: 00087 status() const 00088 { 00089 return "ChunkedHttpHandler"; 00090 } 00091 00092 void 00093 ChunkedHttpHandler:: 00094 doEvent(const char * eventName, 00095 EventType type, 00096 float value, 00097 const char * units) 00098 { 00099 endpoint->doEvent(eventName, type, value, units); 00100 } 00101 00102 std::shared_ptr<ChunkedHttpHandler> 00103 ChunkedHttpHandler:: 00104 makeNewHandlerShared() 00105 { 00106 return endpoint->makeNewHandlerShared(); 00107 } 00108 00109 00110 /*****************************************************************************/ 00111 /* CHUNKED HTTP ENDPOINT */ 00112 /*****************************************************************************/ 00113 00114 ChunkedHttpEndpoint:: 00115 ChunkedHttpEndpoint(const std::string & name, OnChunk onChunk) 00116 : HttpEndpoint("name"), 00117 onChunk(onChunk) 00118 { 00119 // Link up events 00120 onTransportOpen = [=] (TransportBase *) 00121 { 00122 this->doEvent("newConnection"); 00123 }; 00124 00125 onTransportClose = [=] (TransportBase *) 00126 { 00127 this->doEvent("closedConnection"); 00128 }; 00129 } 00130 00131 ChunkedHttpEndpoint:: 00132 ~ChunkedHttpEndpoint() 00133 { 00134 } 00135 00136 std::shared_ptr<ConnectionHandler> 00137 ChunkedHttpEndpoint:: 00138 makeNewHandler() 00139 { 00140 return makeNewHandlerShared(); 00141 } 00142 00143 std::shared_ptr<ChunkedHttpHandler> 00144 ChunkedHttpEndpoint:: 00145 makeNewHandlerShared() 00146 { 00147 return std::make_shared<ChunkedHttpHandler>(); 00148 } 00149 00150 } // namespace Datacratic