9 #include <boost/bind/bind.hpp>
16 #include "http_parser/http_parser.h"
17 #include "sandesh/sandesh_http.h"
18 #include "http/http_log_types.h"
21 using namespace boost::placeholders;
37 http_parser_init(&parser_, HTTP_REQUEST);
41 header_value_.clear();
43 size_t Parse(
const u_int8_t *data,
size_t datalen) {
45 http_parser_execute(&parser_, &settings_,
46 reinterpret_cast<const char *
>(data), datalen);
66 builder->
request_->SetMethod(
static_cast<http_method
>(parser->method));
79 static int OnUrl(
struct http_parser *parser,
80 const char *loc,
size_t length) {
83 builder->
tmp_url_.append(loc, length);
92 const char *loc,
size_t length) {
103 const char *loc,
size_t length) {
110 static int OnBody(
struct http_parser *parser,
111 const char *loc,
size_t length) {
114 builder->
request_->SetBody(loc, length);
119 request_->PushHeader(header_key_, header_value_);
121 header_value_.clear();
124 static struct http_parser_settings settings_;
126 struct http_parser parser_;
135 struct http_parser_settings
HttpSession::RequestBuilder::settings_ = {
147 #define HTTP_SYS_LOG(...)
152 :
Task(req_handler_task_id_, session->GetSessionInstance()),
157 HTTP_SYS_LOG(
"HttpSession", SandeshLevel::UT_INFO,
"RequestHandler destructor");
161 static const char no_response[] =
162 "HTTP/1.1 404 Not Found\r\n"
163 "Content-Type: text/html; charset=UTF-8\r\n"
164 "Content-Length: 46\r\n"
167 "<title>404 Not Found</title>\n"
170 session->
Send(
reinterpret_cast<const u_int8_t *
>(no_response),
171 sizeof(no_response), NULL);
178 return !session_->request_queue_.try_pop(r);
182 "RequestHandler execute");
184 bool del_session =
false;
188 session_->req_queue_empty_ = FromQ(request);
193 if (session_->event_cb_ && !session_->event_cb_.empty()) {
194 session_->event_cb_(session_.get(), request->
Event());
197 session_->set_observer(NULL);
203 if (handler == NULL) {
204 handler = boost::bind(&RequestHandler::NotFound,
207 handler(session_.get(), request);
211 HTTP_SYS_LOG(
"HttpSession", SandeshLevel::UT_INFO,
"DeleteSession "
212 + session_->ToString());
213 session_->set_observer(NULL);
219 std::string
Description()
const {
return "HttpSession::RequestHandler"; }
226 :
SslSession(server, socket, async_ready), event_cb_(NULL) {
295 std::stringstream msg;
297 msg <<
"HttpSession::Read " << size <<
" bytes";
298 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 in a task. Returns true if task is completed. Return false to reschedule the task.
std::string Description() const
Gives a description of the task.
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 class to describe a computational task within OpenSDN control plane applications....
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 std::mutex map_mutex_
boost::intrusive_ptr< HttpSession > HttpSessionPtr
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 std::atomic< long > task_count_
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)
std::atomic< bool > req_queue_empty_
static map_type * GetMap()