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());
TcpSession::Event Event() const
void SetEvent(enum TcpSession::Event event)
std::string UrlPath() const
void SetUrl(std::string *url)
std::string ToString() const
boost::function< void(HttpSession *session, const HttpRequest *)> HttpHandlerFn
HttpHandlerFn GetHandler(const std::string &path)
static int OnBody(struct http_parser *parser, const char *loc, size_t length)
static int OnHeadersComplete(struct http_parser *parser)
static int OnHeaderField(struct http_parser *parser, const char *loc, size_t length)
size_t Parse(const u_int8_t *data, size_t datalen)
static int OnMessageBegin(struct http_parser *parser)
static int OnMessageComplete(struct http_parser *parser)
static int OnUrl(struct http_parser *parser, const char *loc, size_t length)
HttpRequest * GetRequest()
std::unique_ptr< HttpRequest > request_
static int OnStatusComplete(struct http_parser *parser)
static int OnHeaderValue(struct http_parser *parser, const char *loc, size_t length)
virtual bool Run()
Code to execute. Returns true if task is completed. Return false to reschedule the task.
std::string Description() const
RequestHandler(HttpSession *session)
bool FromQ(HttpRequest *&r)
void NotFound(HttpSession *session, const HttpRequest *request)
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > SslSocket
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
int GetTaskId(const std::string &name)
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq....
static TaskScheduler * GetInstance()
Task is a wrapper over tbb::task to support policies.
virtual void DeleteSession(TcpSession *session)
static const uint8_t * BufferData(const Buffer &buffer)
virtual std::string ToString() const
static size_t BufferSize(const Buffer &buffer)
virtual void ReleaseBuffer(Buffer buffer)
void set_observer(EventObserver observer)
boost::asio::const_buffer Buffer
virtual bool Send(const uint8_t *data, size_t size, size_t *sent)
#define HTTP_SYS_LOG(...)
static tbb::atomic< long > task_count_
boost::intrusive_ptr< HttpSession > HttpSessionPtr
tbb::atomic< bool > req_queue_empty_
static map_type * context_map_
void OnSessionEvent(TcpSession *session, enum TcpSession::Event event)
std::map< std::string, HttpSessionPtr > map_type
boost::function< void(HttpSession *session, enum TcpSession::Event event)> SessionEventCb
static tbb::mutex map_mutex_
void RegisterEventCb(SessionEventCb cb)
boost::scoped_ptr< RequestBuilder > request_builder_
tbb::concurrent_queue< HttpRequest * > request_queue_
virtual void OnRead(Buffer buffer)
static int req_handler_task_id_
HttpSession(HttpServer *server, SslSocket *sock, bool async_ready=true)
static map_type * GetMap()