Skip to content

Commit

Permalink
Work on Issue #40.
Browse files Browse the repository at this point in the history
  • Loading branch information
mlucy committed Nov 13, 2012
1 parent ed8fbc4 commit bcef3ee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
19 changes: 11 additions & 8 deletions src/http/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@ http_req_t::resource_t::resource_t(const http_req_t::resource_t &from, const htt
}

http_req_t::resource_t::resource_t(const std::string &_val) {
assign(_val);
if (!assign(_val)) throw std::invalid_argument(_val);
}

http_req_t::resource_t::resource_t(const char * _val, size_t size) {
assign(_val, size);
if (!assign(_val, size)) throw std::invalid_argument(_val);
}

void http_req_t::resource_t::assign(const std::string &_val) {
assign(_val.data(), _val.length());
// Returns false if the assignment fails.
MUST_USE bool http_req_t::resource_t::assign(const std::string &_val) {
return assign(_val.data(), _val.length());
}

void http_req_t::resource_t::assign(const char * _val, size_t size) {
// TODO: Do we actually prevent clients from getting a bad resource path up to here?
guarantee(size > 0 && _val[0] == resource_parts_sep_char[0], "resource path must start with a '/'");
// Returns false if the assignment fails.
MUST_USE bool http_req_t::resource_t::assign(const char * _val, size_t size) {
if (!(size > 0 && _val[0] == resource_parts_sep_char[0])) return false;
val.reset(new char[size]);
memcpy(val.get(), _val, size);
val_size = size;
Expand All @@ -43,6 +44,7 @@ void http_req_t::resource_t::assign(const char * _val, size_t size) {
tokenizer t(val.get() + 1, val.get() + size, resource_parts_sep);
b = t.begin();
e = t.end();
return true;
}

http_req_t::resource_t::iterator http_req_t::resource_t::begin() const {
Expand Down Expand Up @@ -176,6 +178,7 @@ http_res_t http_error_res(const std::string &content, http_status_code_t rescode
return http_res_t(rescode, "application/text", content);
}

//TODO: What the hell is this?
void test_header_parser() {
http_req_t res("/foo/bar");
//str_http_msg_parser_t http_msg_parser;
Expand Down Expand Up @@ -366,7 +369,7 @@ bool tcp_http_msg_parser_t::parse(tcp_conn_t *conn, http_req_t *req, signal_t *c
return false;
}

req->resource.assign(resource_string.resource);
if (!req->resource.assign(resource_string.resource)) return false;
req->query_params = resource_string.query_params;

std::string version_str = parser.readLine(closer);
Expand Down
5 changes: 3 additions & 2 deletions src/http/http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define HTTP_HTTP_HPP_

#include <string>
#include <stdexcept>
#include <vector>

#include "errors.hpp"
Expand Down Expand Up @@ -47,8 +48,8 @@ struct http_req_t {
explicit resource_t(const std::string &_val);
resource_t(const char* _val, size_t size);

void assign(const std::string &_val);
void assign(const char* _val, size_t size);
MUST_USE bool assign(const std::string &_val);
MUST_USE bool assign(const char* _val, size_t size);
iterator begin() const;
iterator end() const;
std::string as_string(const iterator &from) const;
Expand Down

0 comments on commit bcef3ee

Please sign in to comment.