Browse Source

add headers to request object

v0.7.4-release
Ryan 16 years ago
parent
commit
2ee65b8ea2
  1. 12
      count-hosts.js
  2. 123
      server.cc

12
count-hosts.js

@ -8,9 +8,16 @@ function Process(request) {
// //
// request.headers => { "Content-Length": "123" } // request.headers => { "Content-Length": "123" }
// request.http_version => "1.1"
// //
// log("Processing " + request.path + ". method: " + request.method); // log("Processing " + request.path + ". method: " + request.method);
// log("version " + request.http_version);
// log("query_string " + request.query_string);
// log("fragment " + request.fragment);
// log("uri " + request.uri);
// log("headers: " + request.headers.toString());
//for(var f in request.headers) {
//log(f + ": " + request.headers[f]);
//}
var s = ""; var s = "";
// sends null on the last chunk. // sends null on the last chunk.
@ -20,8 +27,11 @@ function Process(request) {
//this.respond(chunk.length.toString(16) + "\r\n" + evalchunk + "\r\n"); //this.respond(chunk.length.toString(16) + "\r\n" + evalchunk + "\r\n");
s += chunk; s += chunk;
} else { } else {
this.respond(encode("hello world\n"));
var output = eval(s); var output = eval(s);
if(output) {
this.respond(encode(output)); this.respond(encode(output));
}
this.respond(encode("\n")); this.respond(encode("\n"));
this.respond("0\r\n\r\n"); this.respond("0\r\n\r\n");
this.respond(null); this.respond(null);

123
server.cc

@ -4,6 +4,7 @@
#include <stdio.h> #include <stdio.h>
#include <assert.h> #include <assert.h>
#include <string> #include <string>
#include <list>
#include <map> #include <map>
#include <v8.h> #include <v8.h>
@ -37,6 +38,12 @@ class HttpRequest {
~HttpRequest(); ~HttpRequest();
string path; string path;
string query_string;
string fragment;
string uri;
list<string> header_fields;
list<string> header_values;
Connection &connection; Connection &connection;
ebb_request parser_info; ebb_request parser_info;
@ -61,7 +68,6 @@ static void make_onBody_callback
{ {
HandleScope handle_scope; HandleScope handle_scope;
Handle<Object> obj = request->js_object; Handle<Object> obj = request->js_object;
// XXX don't always allocate onBody strings // XXX don't always allocate onBody strings
Handle<Value> onBody_val = request->js_object->Get(String::NewSymbol("onBody")); Handle<Value> onBody_val = request->js_object->Get(String::NewSymbol("onBody"));
@ -153,7 +159,7 @@ HttpRequest::~HttpRequest ()
HandleScope scope; HandleScope scope;
// delete a reference to the respond method // delete a reference to the respond method
//js_object->Delete(String::New("respond")); js_object->Delete(String::New("respond"));
js_object.Dispose(); js_object.Dispose();
} }
@ -172,6 +178,68 @@ static void on_path
request->path.append(buf, len); request->path.append(buf, len);
} }
static void on_uri
( ebb_request *req
, const char *buf
, size_t len
)
{
HttpRequest *request = static_cast<HttpRequest*> (req->data);
request->uri.append(buf, len);
}
static void on_query_string
( ebb_request *req
, const char *buf
, size_t len
)
{
HttpRequest *request = static_cast<HttpRequest*> (req->data);
request->query_string.append(buf, len);
}
static void on_fragment
( ebb_request *req
, const char *buf
, size_t len
)
{
HttpRequest *request = static_cast<HttpRequest*> (req->data);
request->fragment.append(buf, len);
}
static void on_header_field
( ebb_request *req
, const char *buf
, size_t len
, int header_index
)
{
HttpRequest *request = static_cast<HttpRequest*> (req->data);
if( request->header_fields.size() == header_index - 1) {
request->header_fields.back().append(buf, len);
} else {
request->header_fields.push_back( string(buf, len) );
}
}
static void on_header_value
( ebb_request *req
, const char *buf
, size_t len
, int header_index
)
{
HttpRequest *request = static_cast<HttpRequest*> (req->data);
if( request->header_values.size() == header_index - 1) {
request->header_values.back().append(buf, len);
} else {
request->header_values.push_back( string(buf, len) );
}
}
static void on_headers_complete static void on_headers_complete
( ebb_request *req ( ebb_request *req
) )
@ -203,10 +271,51 @@ static void on_headers_complete
, String::New(request->path.c_str(), request->path.length()) , String::New(request->path.c_str(), request->path.length())
); );
result->Set ( String::NewSymbol("uri")
, String::New(request->uri.c_str(), request->uri.length())
);
result->Set ( String::NewSymbol("query_string")
, String::New(request->query_string.c_str(), request->query_string.length())
);
result->Set ( String::NewSymbol("fragment")
, String::New(request->fragment.c_str(), request->fragment.length())
);
result->Set ( String::NewSymbol("method") result->Set ( String::NewSymbol("method")
, GetMethodString(request->parser_info.method) , GetMethodString(request->parser_info.method)
); );
char version[10];
snprintf ( version
, 10 // big enough? :)
, "%d.%d"
, request->parser_info.version_major
, request->parser_info.version_minor
);
result->Set ( String::NewSymbol("http_version")
, String::New(version)
);
Handle<Object> headers = Object::New();
list<string>::iterator field_iterator = request->header_fields.begin();
list<string>::iterator value_iterator = request->header_values.begin();
while( value_iterator != request->header_values.end() ) {
string &f = *field_iterator;
string &v = *value_iterator;
headers->Set( String::New(f.c_str(), f.length() )
, String::New(v.c_str(), v.length() )
);
field_iterator++;
value_iterator++;
}
result->Set(String::NewSymbol("headers"), headers);
request->js_object = Persistent<Object>::New(result); request->js_object = Persistent<Object>::New(result);
// Enter this processor's context so all the remaining operations // Enter this processor's context so all the remaining operations
@ -260,11 +369,11 @@ static ebb_request * on_request
HttpRequest *request = new HttpRequest(*connection); HttpRequest *request = new HttpRequest(*connection);
request->parser_info.on_path = on_path; request->parser_info.on_path = on_path;
request->parser_info.on_query_string = NULL; request->parser_info.on_query_string = on_query_string;
request->parser_info.on_uri = NULL; request->parser_info.on_uri = on_uri;
request->parser_info.on_fragment = NULL; request->parser_info.on_fragment = on_fragment;
request->parser_info.on_header_field = NULL; request->parser_info.on_header_field = on_header_field;
request->parser_info.on_header_value = NULL; request->parser_info.on_header_value = on_header_value;
request->parser_info.on_headers_complete = on_headers_complete; request->parser_info.on_headers_complete = on_headers_complete;
request->parser_info.on_body = on_body; request->parser_info.on_body = on_body;
request->parser_info.on_complete = on_request_complete; request->parser_info.on_complete = on_request_complete;

Loading…
Cancel
Save