8 #include <boost/bind.hpp>
15 #include "http_parser/http_parser.h"
16 #include "sandesh/sandesh_http.h"
17 #include "http/http_log_types.h"
35 http_parser_init(&parser_, HTTP_REQUEST);
39 header_value_.clear();
41 size_t Parse(
const u_int8_t *data,
size_t datalen) {
43 http_parser_execute(&parser_, &settings_,
44 reinterpret_cast<const char *>(data), datalen);
64 builder->
request_->SetMethod(static_cast<http_method>(parser->method));
77 static int OnUrl(
struct http_parser *parser,
78 const char *loc,
size_t length) {
81 builder->
tmp_url_.append(loc, length);
90 const char *loc,
size_t length) {
101 const char *loc,
size_t length) {
108 static int OnBody(
struct http_parser *parser,
109 const char *loc,
size_t length) {
112 builder->
request_->SetBody(loc, length);
117 request_->PushHeader(header_key_, header_value_);
119 header_value_.clear();
122 static struct http_parser_settings settings_;
124 struct http_parser parser_;
133 struct http_parser_settings
HttpSession::RequestBuilder::settings_ = {
145 #define HTTP_SYS_LOG(...)
150 :
Task(req_handler_task_id_, session->GetSessionInstance()),
155 HTTP_SYS_LOG(
"HttpSession", SandeshLevel::UT_INFO,
"RequestHandler destructor");
159 static const char no_response[] =
160 "HTTP/1.1 404 Not Found\r\n"
161 "Content-Type: text/html; charset=UTF-8\r\n"
162 "Content-Length: 46\r\n"
165 "<title>404 Not Found</title>\n"
168 session->
Send(reinterpret_cast<const u_int8_t *>(no_response),
169 sizeof(no_response), NULL);
176 return !session_->request_queue_.try_pop(r);
180 "RequestHandler execute");
182 bool del_session =
false;
186 session_->req_queue_empty_ = FromQ(request);
191 if (session_->event_cb_ && !session_->event_cb_.empty()) {
192 session_->event_cb_(session_.get(), request->
Event());
195 session_->set_observer(NULL);
201 if (handler == NULL) {
202 handler = boost::bind(&RequestHandler::NotFound,
205 handler(session_.get(), request);
209 HTTP_SYS_LOG(
"HttpSession", SandeshLevel::UT_INFO,
"DeleteSession "
210 + session_->ToString());
211 session_->set_observer(NULL);
217 std::string
Description()
const {
return "HttpSession::RequestHandler"; }
224 :
SslSession(server, socket, async_ready), event_cb_(NULL) {
292 std::stringstream msg;
294 msg <<
"HttpSession::Read " << size <<
" bytes";
295 HTTP_SYS_LOG(
"HttpSession", SandeshLevel::UT_DEBUG, msg.str());
static int req_handler_task_id_
std::string Description() const
boost::asio::const_buffer Buffer
virtual void DeleteSession(TcpSession *session)
tbb::atomic< bool > req_queue_empty_
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
static int OnUrl(struct http_parser *parser, const char *loc, size_t length)
static map_type * GetMap()
virtual std::string ToString() const
static tbb::mutex map_mutex_
static size_t BufferSize(const Buffer &buffer)
std::unique_ptr< HttpRequest > request_
boost::intrusive_ptr< HttpSession > HttpSessionPtr
std::map< std::string, HttpSessionPtr > map_type
void NotFound(HttpSession *session, const HttpRequest *request)
boost::function< void(HttpSession *session, enum TcpSession::Event event)> SessionEventCb
static int OnBody(struct http_parser *parser, const char *loc, size_t length)
virtual bool Send(const uint8_t *data, size_t size, size_t *sent)
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > SslSocket
int GetTaskId(const std::string &name)
void set_observer(EventObserver observer)
boost::function< void(HttpSession *session, const HttpRequest *)> HttpHandlerFn
bool FromQ(HttpRequest *&r)
HttpRequest * GetRequest()
static TaskScheduler * GetInstance()
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq...
virtual void OnRead(Buffer buffer)
virtual bool Run()
Code to execute. Returns true if task is completed. Return false to reschedule the task...
void SetEvent(enum TcpSession::Event event)
HttpSession(HttpServer *server, SslSocket *sock, bool async_ready=true)
size_t Parse(const u_int8_t *data, size_t datalen)
static map_type * context_map_
void OnSessionEvent(TcpSession *session, enum TcpSession::Event event)
HttpHandlerFn GetHandler(const std::string &path)
boost::scoped_ptr< RequestBuilder > request_builder_
static tbb::atomic< long > task_count_
static int OnHeadersComplete(struct http_parser *parser)
static int OnMessageBegin(struct http_parser *parser)
virtual void ReleaseBuffer(Buffer buffer)
std::string ToString() const
static int OnHeaderField(struct http_parser *parser, const char *loc, size_t length)
tbb::concurrent_queue< HttpRequest * > request_queue_
static int OnMessageComplete(struct http_parser *parser)
static const uint8_t * BufferData(const Buffer &buffer)
TcpSession::Event Event() const
Task is a wrapper over tbb::task to support policies.
RequestHandler(HttpSession *session)
std::string UrlPath() const
void RegisterEventCb(SessionEventCb cb)
void SetUrl(std::string *url)
#define HTTP_SYS_LOG(...)
static int OnStatusComplete(struct http_parser *parser)
static int OnHeaderValue(struct http_parser *parser, const char *loc, size_t length)