Skip to content

Commit

Permalink
#17 #21 code improve, test update
Browse files Browse the repository at this point in the history
  • Loading branch information
dedok committed Nov 15, 2012
1 parent e183d13 commit 959636d
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 86 deletions.
44 changes: 31 additions & 13 deletions src/common/http_utility/http_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,25 +131,43 @@ bool http_translate_range_header(range_header & rheader, header_list_type const
} else delim_pos = end;
if (state)
return http_range_header_value_parser(rheader, header.value, start + 1, delim_pos, false);
} else {
boost::tie(state, header) = http_get_header(headers, "Accept");
if (state) {
// TODO fix this
rheader.bstart_1 = rheader.bend_1 = range_header::all;
return true;
}
} // if
}

return false;
}

bool http_translate_range_header(range_header & rheader, char const * header)
static bool http_translate_range_header_(range_header & rheader, std::string const & header)
{
bool state = true;
std::string::size_type const end = header.size();
std::string::size_type start = std::string::npos, delim_pos = std::string::npos;

if ((start = header.find_first_of("=")) == std::string::npos)
return false;

if ((delim_pos = header.find_first_of(",")) != std::string::npos) {
if (delim_pos + 1 == end)
return false;
state = http_range_header_value_parser(rheader, header, delim_pos + 1, end, true);
delim_pos = delim_pos;
}
else
delim_pos = end;

if (state)
state = http_range_header_value_parser(rheader, header, start + 1, delim_pos, false);

return state;
}

bool http_translate_range_header(range_header & rheader, std::string const & header)
{
rheader.bstart_1 = rheader.bend_1 = rheader.bstart_2 = rheader.bend_1 = 0;
return (std::sscanf(header,
"bytes=" LC_INTMAX_SF "-" LC_INTMAX_SF,
rheader.bstart_1 = rheader.bend_1 = rheader.bstart_2 = rheader.bend_1 = range_header::bad;
#if defined(LC_USE_CF_UNSAFE_FUNCTIONS)
return (std::sscanf(header.c_str(), "bytes=" LC_INTMAX_SF "-" LC_INTMAX_SF,
&rheader.bstart_1, &rheader.bend_1) > 0 ? true : false);
return false;
#endif
return http_translate_range_header_(rheader, header);
}

bool http_translate_accept_header(range_header & rheader, char const * header)
Expand Down
9 changes: 6 additions & 3 deletions src/common/http_utility/http_utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace utility {
struct range_header
{
enum {
all = -2, // For start means 0 for end means file size
bad = -1 // This value set when parsing failed(not valid requst)
all = -1, // For start means 0 for end means file size
bad = -2 // This value set when parsing failed(not valid requst)
};

boost::int64_t bstart_1;
Expand All @@ -42,7 +42,10 @@ boost::tuple<bool, http_header> http_get_header(std::string const & name);

bool http_translate_range_header(range_header & rheader, header_list_type const & headers);

bool http_translate_range_header(range_header & rheader, char const * range_header);
bool http_translate_range_header(range_header & rheader, std::string const & range_header);

inline bool http_translate_range_header_c(range_header & rheader, std::string const & range_header)
{ return http_translate_range_header(rheader, range_header); }

bool http_translate_accept_header(range_header & rheader, char const * range_header);

Expand Down
106 changes: 80 additions & 26 deletions src/common/network/details/http_mongoose_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,27 @@ static inline bool mon_is_range_request(

char const * range_header = mg_get_header(conn, "Range");
if (range_header)
return utility::http_translate_range_header(rheader, range_header);

char const * accept_header = mg_get_header(conn, "Accept");
if (accept_header)
return utility::http_translate_accept_header(rheader, range_header);
return utility::http_translate_range_header_c(rheader, range_header);

return false;
}

static inline bool mon_is_head_request(struct mg_connection * conn, struct mg_request_info const * ri)
{
BOOST_ASSERT(ri != NULL);
bool const s = ((strcmp(ri->http_version, "1.1") != 0 | (strcmp(ri->http_version, "1.0") != 0)) &
strcmp(ri->request_method, "HEAD") != 0);
return s;
if ((strcmp(ri->http_version, "1.1") != 0 || (strcmp(ri->http_version, "1.0") == 0)))
if (strcmp(ri->request_method, "HEAD") == 0)
return true;
return false;
}

static inline bool mon_is_content_requst(struct mg_connection * conn, struct mg_request_info const * ri)
{
BOOST_ASSERT(ri != NULL);
bool const s = ((strcmp(ri->http_version, "1.1") != 0 | strcmp(ri->http_version, "1.0") != 0) &
(strcmp(ri->request_method, "GET") != 0 | strcmp(ri->request_method, "POST") != 0));
return s;
if ((strcmp(ri->http_version, "1.1") == 0 || strcmp(ri->http_version, "1.0") == 0))
if ((strcmp(ri->request_method, "GET") == 0 || strcmp(ri->request_method, "POST") == 0))
return true;
return false;
}

static void * mongoose_completion_routine(enum mg_event event, struct mg_connection * conn)
Expand Down Expand Up @@ -89,11 +87,14 @@ static void * http_stock_reply(
case http_transport_event_handler::not_found :
reply = stock_replies::cast_to_string(http_reply::not_found);
break;
case http_transport_event_handler::bad_request :
reply = stock_replies::cast_to_string(http_reply::bad_request);
break;
}

mg_write(conn, reply.c_str(), reply.size());

far_handler->error(status, uri.c_str());

return NOT_NULL;
}

Expand Down Expand Up @@ -124,9 +125,11 @@ static void * far_handler_on_range_request(
bytes_writed,
uri.c_str());

if (pcd.op_status != http_transport_event_handler::ok)
return http_stock_reply(conn, pcd.op_status, far_handler, ri);

if (pcd.op_status != http_transport_event_handler::ok) {
far_handler->error(pcd.op_status, uri.c_str());
break;
} // if

if ((bytes_writed = mg_write(conn, &pcd.io_buffer.at(0), pcd.last_readed)) <= 0) {
far_handler->error(http_transport_event_handler::write_op_error, uri.c_str());
break;
Expand All @@ -136,26 +139,77 @@ static void * far_handler_on_range_request(
break;
} // read & send loop
return NOT_NULL;
}
} // if

LC_WARNING("writing header data failed, for uri '%s'", uri.c_str())
return NOT_NULL;
}

static void * far_handler_on_head_request(
transport_context_ptr far_handler, struct mg_connection * conn, struct mg_request_info const * ri)
http_transport_event_handler_ptr far_handler, struct mg_connection * conn, struct mg_request_info const * ri)
{
BOOST_ASSERT(ri != NULL);
// TODO impl this
return NULL;

http_transport_event_handler::http_data hd;
std::string const uri = utility::http_normalize_uri_c(ri->uri);

far_handler->on_get_head_headers(hd, uri.c_str());
if (hd.op_status != http_transport_event_handler::ok)
return http_stock_reply(conn, hd.op_status, far_handler, ri);

if (mg_write(conn, hd.reply_header.c_str(), hd.reply_header.size()) <= 0)
far_handler->error(http_transport_event_handler::write_op_error, uri.c_str());

return NOT_NULL;
}

static void * far_handler_on_content_request(
transport_context_ptr far_handler, struct mg_connection * conn, struct mg_request_info const * ri)
http_transport_event_handler_ptr far_handler, struct mg_connection * conn, struct mg_request_info const * ri)
{
BOOST_ASSERT(ri != NULL);
// TODO impl this
return NULL;

http_transport_event_handler::http_data hd;
std::string const uri = utility::http_normalize_uri_c(ri->uri);

far_handler->on_get_content_headers(hd, uri.c_str());
if (hd.op_status != http_transport_event_handler::ok)
return http_stock_reply(conn, hd.op_status, far_handler, ri);

if (mg_write(conn, hd.reply_header.c_str(), hd.reply_header.size()) > 0) {
bool has_data = true;
hd.seek_offset_pos = 0;
for (boost::int64_t bytes_writed = 0;;)
{
has_data = far_handler->on_get_content_body(hd,
0, /* from 0 pos */
-1, /* means to whole file */
bytes_writed,
uri.c_str());

if (hd.op_status != http_transport_event_handler::ok) {
far_handler->error(hd.op_status, uri.c_str());
break;
}

if ((bytes_writed = mg_write(conn, &hd.io_buffer.at(0), hd.last_readed)) <= 0) {
far_handler->error(http_transport_event_handler::write_op_error, uri.c_str());
break;
} // if

if (bytes_writed != hd.last_readed) {
far_handler->error(http_transport_event_handler::write_op_error, uri.c_str());
break;
} // if

if (!has_data)
break;
} // read & send loop

return NOT_NULL;
} // if

LC_WARNING("writing header data failed, for uri '%s'", uri.c_str())
return NOT_NULL;
}


Expand Down Expand Up @@ -251,17 +305,17 @@ void * http_mongoose_transport::dispatch_http_message(
case MG_NEW_REQUEST :
if (mon_is_range_request(conn, ri, rheader))
return far_handler_on_range_request(http_context_, conn, ri, rheader);
else if(mon_is_head_request(conn, ri))
if (mon_is_head_request(conn, ri))
return far_handler_on_head_request(http_context_, conn, ri);
else if (mon_is_content_requst(conn, ri))
if (mon_is_content_requst(conn, ri))
return far_handler_on_content_request(http_context_, conn, ri);
break;
return http_stock_reply(conn, http_transport_event_handler::bad_request, http_context_, ri);

case MG_HTTP_ERROR :
return NULL;

case MG_REQUEST_COMPLETE :
return NULL;
return NULL ;

default :
break;
Expand Down
3 changes: 2 additions & 1 deletion src/common/network/details/http_transport_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public :
ok = 0x0,
io_error = 0x2,
not_found = 0x1,
write_op_error = 0x3,
bad_request = 0x3,
write_op_error = 0x4,
unknown = write_op_error + 0x1
};

Expand Down
2 changes: 1 addition & 1 deletion src/common/notification_model/notification_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void notification_unit::execution_loop()
has_pending_notifications = copy_pending_notifications())
{
{ // stop_work_ lock zone
boost::lock_guard<boost::mutex> guard(pn_lock_);
boost::lock_guard<boost::mutex> stop_guard(pn_lock_);
if (stop_work_) break;
} // stop_work_ lock zone ned
if (has_pending_notifications)
Expand Down
Loading

0 comments on commit 959636d

Please sign in to comment.