2009-03-03 01:56:15 +01:00
|
|
|
#include "node_http.h"
|
2009-03-04 00:31:37 +01:00
|
|
|
#include "node.h"
|
2009-02-20 16:18:44 +01:00
|
|
|
|
2009-03-03 01:56:15 +01:00
|
|
|
#include <oi_socket.h>
|
|
|
|
#include <ebb_request_parser.h>
|
2009-03-02 15:49:24 +01:00
|
|
|
|
2009-02-16 01:34:45 +01:00
|
|
|
#include <string>
|
2009-02-24 19:13:58 +01:00
|
|
|
#include <list>
|
2009-02-16 01:34:45 +01:00
|
|
|
|
2009-04-04 14:50:15 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2009-02-16 01:34:45 +01:00
|
|
|
using namespace v8;
|
|
|
|
using namespace std;
|
|
|
|
|
2009-03-03 01:56:15 +01:00
|
|
|
static Persistent<ObjectTemplate> request_template;
|
2009-02-16 01:34:45 +01:00
|
|
|
|
2009-03-03 02:15:26 +01:00
|
|
|
// globals
|
|
|
|
static Persistent<String> path_str;
|
|
|
|
static Persistent<String> uri_str;
|
|
|
|
static Persistent<String> query_string_str;
|
|
|
|
static Persistent<String> fragment_str;
|
|
|
|
static Persistent<String> method_str;
|
|
|
|
static Persistent<String> http_version_str;
|
2009-03-05 14:45:11 +01:00
|
|
|
static Persistent<String> headers_str;
|
2009-03-03 02:15:26 +01:00
|
|
|
|
|
|
|
static Persistent<String> on_request_str;
|
|
|
|
static Persistent<String> on_body_str;
|
|
|
|
static Persistent<String> respond_str;
|
|
|
|
|
|
|
|
static Persistent<String> copy_str;
|
|
|
|
static Persistent<String> delete_str;
|
|
|
|
static Persistent<String> get_str;
|
|
|
|
static Persistent<String> head_str;
|
|
|
|
static Persistent<String> lock_str;
|
|
|
|
static Persistent<String> mkcol_str;
|
|
|
|
static Persistent<String> move_str;
|
|
|
|
static Persistent<String> options_str;
|
|
|
|
static Persistent<String> post_str;
|
|
|
|
static Persistent<String> propfind_str;
|
|
|
|
static Persistent<String> proppatch_str;
|
|
|
|
static Persistent<String> put_str;
|
|
|
|
static Persistent<String> trace_str;
|
|
|
|
static Persistent<String> unlock_str;
|
|
|
|
|
2009-03-15 11:11:51 +01:00
|
|
|
#define INVALID_STATE_ERR 1
|
|
|
|
|
2009-03-03 01:56:15 +01:00
|
|
|
class Server {
|
|
|
|
public:
|
|
|
|
Server (Handle<Object> _js_server);
|
|
|
|
~Server ();
|
|
|
|
|
|
|
|
int Start(struct addrinfo *servinfo);
|
|
|
|
void Stop();
|
|
|
|
|
|
|
|
Handle<Value> Callback()
|
|
|
|
{
|
|
|
|
HandleScope scope;
|
2009-03-03 02:15:26 +01:00
|
|
|
Handle<Value> value = js_server->Get(on_request_str);
|
2009-03-03 01:56:15 +01:00
|
|
|
return scope.Close(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
oi_server server;
|
|
|
|
Persistent<Object> js_server;
|
|
|
|
};
|
2009-02-23 13:48:34 +01:00
|
|
|
|
2009-03-06 19:49:52 +01:00
|
|
|
class HttpRequest;
|
|
|
|
|
2009-02-23 13:48:34 +01:00
|
|
|
class Connection {
|
|
|
|
public:
|
2009-03-06 19:49:52 +01:00
|
|
|
Connection();
|
|
|
|
~Connection();
|
|
|
|
|
|
|
|
void Parse(const void *buf, size_t count);
|
|
|
|
void Write();
|
2009-03-07 17:08:35 +01:00
|
|
|
void Close();
|
|
|
|
void AddRequest (HttpRequest *request);
|
2009-03-06 19:49:52 +01:00
|
|
|
|
2009-02-23 13:48:34 +01:00
|
|
|
oi_socket socket;
|
2009-03-06 19:49:52 +01:00
|
|
|
Persistent<Function> js_onrequest;
|
2009-03-03 01:56:15 +01:00
|
|
|
|
2009-03-06 19:49:52 +01:00
|
|
|
private:
|
|
|
|
ebb_request_parser parser;
|
|
|
|
list<HttpRequest*> requests;
|
2009-03-07 17:08:35 +01:00
|
|
|
list<HttpRequest*> finished_requests;
|
2009-03-03 01:56:15 +01:00
|
|
|
friend class Server;
|
2009-02-23 13:48:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class HttpRequest {
|
|
|
|
public:
|
|
|
|
HttpRequest (Connection &c);
|
2009-03-06 19:49:52 +01:00
|
|
|
/* Deleted from C++ as soon as possible.
|
|
|
|
* Javascript object might linger. This is okay
|
|
|
|
*/
|
2009-02-23 21:04:30 +01:00
|
|
|
~HttpRequest();
|
2009-02-23 13:48:34 +01:00
|
|
|
|
2009-03-03 01:56:15 +01:00
|
|
|
void MakeBodyCallback (const char *base, size_t length);
|
2009-03-03 02:36:08 +01:00
|
|
|
Local<Object> CreateJSObject ();
|
2009-03-06 19:49:52 +01:00
|
|
|
void Respond (Handle<Value> data);
|
2009-03-03 01:56:15 +01:00
|
|
|
|
2009-02-23 13:48:34 +01:00
|
|
|
string path;
|
2009-02-24 19:13:58 +01:00
|
|
|
string query_string;
|
|
|
|
string fragment;
|
|
|
|
string uri;
|
|
|
|
|
|
|
|
list<string> header_fields;
|
|
|
|
list<string> header_values;
|
2009-02-23 13:48:34 +01:00
|
|
|
|
|
|
|
Connection &connection;
|
|
|
|
ebb_request parser_info;
|
2009-03-06 19:49:52 +01:00
|
|
|
|
|
|
|
list<oi_buf*> output;
|
|
|
|
bool done;
|
2009-02-23 13:48:34 +01:00
|
|
|
Persistent<Object> js_object;
|
|
|
|
};
|
|
|
|
|
2009-03-03 19:17:33 +01:00
|
|
|
static Handle<Value>
|
|
|
|
GetMethodString (int method)
|
2009-02-23 13:48:34 +01:00
|
|
|
{
|
2009-02-23 21:04:30 +01:00
|
|
|
switch(method) {
|
2009-03-03 02:15:26 +01:00
|
|
|
case EBB_COPY: return copy_str;
|
|
|
|
case EBB_DELETE: return delete_str;
|
|
|
|
case EBB_GET: return get_str;
|
|
|
|
case EBB_HEAD: return head_str;
|
|
|
|
case EBB_LOCK: return lock_str;
|
|
|
|
case EBB_MKCOL: return mkcol_str;
|
|
|
|
case EBB_MOVE: return move_str;
|
|
|
|
case EBB_OPTIONS: return options_str;
|
|
|
|
case EBB_POST: return post_str;
|
|
|
|
case EBB_PROPFIND: return propfind_str;
|
|
|
|
case EBB_PROPPATCH: return proppatch_str;
|
|
|
|
case EBB_PUT: return put_str;
|
|
|
|
case EBB_TRACE: return trace_str;
|
|
|
|
case EBB_UNLOCK: return unlock_str;
|
2009-02-23 13:48:34 +01:00
|
|
|
}
|
2009-02-23 21:04:30 +01:00
|
|
|
return Null();
|
2009-02-23 13:48:34 +01:00
|
|
|
}
|
|
|
|
|
2009-03-03 19:17:33 +01:00
|
|
|
static Handle<Value>
|
|
|
|
RespondCallback (const Arguments& args)
|
2009-02-23 13:48:34 +01:00
|
|
|
{
|
|
|
|
HandleScope scope;
|
2009-03-03 19:17:33 +01:00
|
|
|
|
2009-03-09 14:09:09 +01:00
|
|
|
Handle<Value> v = args.Holder()->GetInternalField(0);
|
|
|
|
if(v->IsUndefined()) {
|
2009-03-15 11:11:51 +01:00
|
|
|
// check that args.Holder()->GetInternalField(0)
|
|
|
|
// is not NULL if so raise INVALID_STATE_ERR
|
2009-03-09 14:09:09 +01:00
|
|
|
printf("null request external\n");
|
2009-03-15 11:11:51 +01:00
|
|
|
ThrowException(Integer::New(INVALID_STATE_ERR));
|
2009-03-09 14:09:09 +01:00
|
|
|
return Undefined();
|
|
|
|
}
|
|
|
|
Handle<External> field = Handle<External>::Cast(v);
|
2009-03-03 19:17:33 +01:00
|
|
|
HttpRequest* request = static_cast<HttpRequest*>(field->Value());
|
2009-03-06 19:49:52 +01:00
|
|
|
request->Respond(args[0]);
|
2009-03-09 14:09:09 +01:00
|
|
|
return Undefined();
|
2009-03-06 19:49:52 +01:00
|
|
|
}
|
2009-03-03 19:17:33 +01:00
|
|
|
|
2009-03-06 19:49:52 +01:00
|
|
|
void
|
|
|
|
HttpRequest::Respond (Handle<Value> data)
|
|
|
|
{
|
|
|
|
if(data == Null()) {
|
|
|
|
done = true;
|
|
|
|
} else {
|
|
|
|
Handle<String> s = data->ToString();
|
2009-03-09 17:26:42 +01:00
|
|
|
|
2009-04-04 14:50:15 +02:00
|
|
|
size_t l1 = s->Utf8Length(), l2;
|
|
|
|
oi_buf *buf = oi_buf_new2(l1);
|
|
|
|
l2 = s->WriteUtf8(buf->base, l1);
|
|
|
|
assert(l1 == l2);
|
2009-03-09 17:26:42 +01:00
|
|
|
|
2009-03-06 19:49:52 +01:00
|
|
|
output.push_back(buf);
|
|
|
|
}
|
|
|
|
connection.Write();
|
|
|
|
}
|
2009-02-23 13:48:34 +01:00
|
|
|
|
|
|
|
|
2009-03-03 19:17:33 +01:00
|
|
|
static void
|
|
|
|
on_path (ebb_request *req, const char *buf, size_t len)
|
2009-02-23 21:04:30 +01:00
|
|
|
{
|
|
|
|
HttpRequest *request = static_cast<HttpRequest*> (req->data);
|
|
|
|
request->path.append(buf, len);
|
|
|
|
}
|
|
|
|
|
2009-03-03 19:17:33 +01:00
|
|
|
static void
|
|
|
|
on_uri (ebb_request *req, const char *buf, size_t len)
|
2009-02-24 19:13:58 +01:00
|
|
|
{
|
|
|
|
HttpRequest *request = static_cast<HttpRequest*> (req->data);
|
|
|
|
request->uri.append(buf, len);
|
|
|
|
}
|
|
|
|
|
2009-03-03 19:17:33 +01:00
|
|
|
static void
|
|
|
|
on_query_string (ebb_request *req, const char *buf, size_t len)
|
2009-02-24 19:13:58 +01:00
|
|
|
{
|
|
|
|
HttpRequest *request = static_cast<HttpRequest*> (req->data);
|
|
|
|
request->query_string.append(buf, len);
|
|
|
|
}
|
|
|
|
|
2009-03-03 19:17:33 +01:00
|
|
|
static void
|
|
|
|
on_fragment (ebb_request *req, const char *buf, size_t len)
|
2009-02-24 19:13:58 +01:00
|
|
|
{
|
|
|
|
HttpRequest *request = static_cast<HttpRequest*> (req->data);
|
|
|
|
request->fragment.append(buf, len);
|
|
|
|
}
|
|
|
|
|
2009-03-09 14:09:09 +01:00
|
|
|
static const char upcase[] =
|
|
|
|
"\0______________________________"
|
|
|
|
"_________________0123456789_____"
|
|
|
|
"__ABCDEFGHIJKLMNOPQRSTUVWXYZ____"
|
|
|
|
"__ABCDEFGHIJKLMNOPQRSTUVWXYZ____"
|
|
|
|
"________________________________"
|
|
|
|
"________________________________"
|
|
|
|
"________________________________"
|
|
|
|
"________________________________";
|
|
|
|
|
2009-03-03 19:17:33 +01:00
|
|
|
static void
|
|
|
|
on_header_field (ebb_request *req, const char *buf, size_t len, int header_index)
|
2009-02-24 19:13:58 +01:00
|
|
|
{
|
|
|
|
HttpRequest *request = static_cast<HttpRequest*> (req->data);
|
|
|
|
|
2009-03-09 14:09:09 +01:00
|
|
|
char upbuf[len];
|
|
|
|
|
|
|
|
for(int i = 0; i < len; i++)
|
|
|
|
upbuf[i] = upcase[buf[i]];
|
|
|
|
|
2009-02-24 19:13:58 +01:00
|
|
|
if( request->header_fields.size() == header_index - 1) {
|
2009-03-09 14:09:09 +01:00
|
|
|
request->header_fields.back().append(upbuf, len);
|
2009-02-24 19:13:58 +01:00
|
|
|
} else {
|
2009-03-09 14:09:09 +01:00
|
|
|
request->header_fields.push_back( string(upbuf, len) );
|
2009-02-24 19:13:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-03 19:17:33 +01:00
|
|
|
static void
|
|
|
|
on_header_value (ebb_request *req, const char *buf, size_t len, int header_index)
|
2009-02-24 19:13:58 +01:00
|
|
|
{
|
|
|
|
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) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-03 19:17:33 +01:00
|
|
|
static void
|
|
|
|
on_headers_complete (ebb_request *req)
|
2009-02-23 21:04:30 +01:00
|
|
|
{
|
|
|
|
HttpRequest *request = static_cast<HttpRequest*> (req->data);
|
2009-02-23 13:48:34 +01:00
|
|
|
|
2009-03-03 02:36:08 +01:00
|
|
|
HandleScope scope;
|
2009-02-23 13:48:34 +01:00
|
|
|
|
2009-03-09 14:09:09 +01:00
|
|
|
Handle<Object> js_request = request->CreateJSObject();
|
2009-02-23 13:48:34 +01:00
|
|
|
|
|
|
|
// Set up an exception handler before calling the Process function
|
|
|
|
TryCatch try_catch;
|
|
|
|
|
|
|
|
// Invoke the process function, giving the global object as 'this'
|
|
|
|
// and one argument, the request.
|
|
|
|
const int argc = 1;
|
2009-03-03 02:36:08 +01:00
|
|
|
Handle<Value> argv[argc] = { js_request };
|
2009-03-06 19:49:52 +01:00
|
|
|
Handle<Value> r = request->connection.js_onrequest->Call(Context::GetCurrent()->Global(), argc, argv);
|
2009-03-04 00:31:37 +01:00
|
|
|
|
|
|
|
if(try_catch.HasCaught())
|
|
|
|
node_fatal_exception(try_catch);
|
2009-02-23 13:48:34 +01:00
|
|
|
}
|
2009-02-21 21:00:40 +01:00
|
|
|
|
2009-03-03 19:17:33 +01:00
|
|
|
static void
|
|
|
|
on_request_complete (ebb_request *req)
|
2009-02-23 13:48:34 +01:00
|
|
|
{
|
|
|
|
HttpRequest *request = static_cast<HttpRequest*> (req->data);
|
2009-03-03 01:56:15 +01:00
|
|
|
request->MakeBodyCallback(NULL, 0); // EOF
|
2009-02-20 16:18:44 +01:00
|
|
|
}
|
|
|
|
|
2009-03-03 19:17:33 +01:00
|
|
|
static void
|
|
|
|
on_body (ebb_request *req, const char *base, size_t length)
|
2009-02-20 16:18:44 +01:00
|
|
|
{
|
2009-02-21 21:00:40 +01:00
|
|
|
HttpRequest *request = static_cast<HttpRequest*> (req->data);
|
2009-02-23 13:48:34 +01:00
|
|
|
|
2009-02-23 21:04:30 +01:00
|
|
|
if(length)
|
2009-03-03 01:56:15 +01:00
|
|
|
request->MakeBodyCallback(base, length);
|
2009-02-16 01:34:45 +01:00
|
|
|
}
|
|
|
|
|
2009-02-23 21:04:30 +01:00
|
|
|
static ebb_request * on_request
|
2009-02-16 01:34:45 +01:00
|
|
|
( void *data
|
|
|
|
)
|
|
|
|
{
|
|
|
|
Connection *connection = static_cast<Connection*> (data);
|
|
|
|
|
2009-03-07 17:08:35 +01:00
|
|
|
HttpRequest *request = new HttpRequest(*connection);
|
|
|
|
connection->AddRequest(request);
|
2009-02-16 01:34:45 +01:00
|
|
|
|
|
|
|
return &request->parser_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void on_read
|
|
|
|
( oi_socket *socket
|
|
|
|
, const void *buf
|
|
|
|
, size_t count
|
|
|
|
)
|
|
|
|
{
|
|
|
|
Connection *connection = static_cast<Connection*> (socket->data);
|
2009-03-07 17:08:35 +01:00
|
|
|
if(count == 0) {
|
|
|
|
connection->Close();
|
|
|
|
} else {
|
|
|
|
//write(1, buf, count);
|
|
|
|
connection->Parse(buf, count);
|
|
|
|
}
|
2009-02-16 01:34:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void on_close
|
|
|
|
( oi_socket *socket
|
|
|
|
)
|
|
|
|
{
|
|
|
|
Connection *connection = static_cast<Connection*> (socket->data);
|
2009-02-23 21:04:30 +01:00
|
|
|
delete connection;
|
2009-02-16 01:34:45 +01:00
|
|
|
}
|
|
|
|
|
2009-03-03 19:17:33 +01:00
|
|
|
HttpRequest::~HttpRequest ()
|
|
|
|
{
|
2009-03-09 14:09:09 +01:00
|
|
|
HandleScope scope; // needed?
|
2009-03-06 19:49:52 +01:00
|
|
|
// delete a reference c++ HttpRequest
|
2009-03-07 17:08:35 +01:00
|
|
|
js_object->SetInternalField(0, Undefined());
|
2009-03-15 11:11:51 +01:00
|
|
|
js_object->Delete(respond_str);
|
2009-03-06 19:49:52 +01:00
|
|
|
// dispose of Persistent handle so that
|
|
|
|
// it can be GC'd normally.
|
2009-03-03 19:17:33 +01:00
|
|
|
js_object.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpRequest::HttpRequest (Connection &c) : connection(c)
|
|
|
|
{
|
|
|
|
ebb_request_init(&parser_info);
|
|
|
|
parser_info.on_path = on_path;
|
|
|
|
parser_info.on_query_string = on_query_string;
|
|
|
|
parser_info.on_uri = on_uri;
|
|
|
|
parser_info.on_fragment = on_fragment;
|
|
|
|
parser_info.on_header_field = on_header_field;
|
|
|
|
parser_info.on_header_value = on_header_value;
|
|
|
|
parser_info.on_headers_complete = on_headers_complete;
|
|
|
|
parser_info.on_body = on_body;
|
|
|
|
parser_info.on_complete = on_request_complete;
|
|
|
|
parser_info.data = this;
|
2009-03-06 19:49:52 +01:00
|
|
|
|
|
|
|
done = false;
|
2009-03-03 19:17:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HttpRequest::MakeBodyCallback (const char *base, size_t length)
|
|
|
|
{
|
|
|
|
HandleScope handle_scope;
|
2009-03-07 17:08:35 +01:00
|
|
|
|
2009-03-06 19:49:52 +01:00
|
|
|
Handle<Value> onbody_val = js_object->Get(on_body_str);
|
|
|
|
if (!onbody_val->IsFunction()) return;
|
|
|
|
Handle<Function> onbody = Handle<Function>::Cast(onbody_val);
|
2009-03-03 19:17:33 +01:00
|
|
|
|
|
|
|
TryCatch try_catch;
|
|
|
|
const int argc = 1;
|
|
|
|
Handle<Value> argv[argc];
|
|
|
|
|
|
|
|
if(length) {
|
2009-03-07 17:08:35 +01:00
|
|
|
// TODO ByteArray?
|
2009-03-09 17:26:42 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
uint16_t expanded_base[length];
|
|
|
|
for(int i = 0; i < length; i++) {
|
|
|
|
expanded_base[i] = base[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<String> chunk = String::New(expanded_base, length);
|
2009-03-03 19:17:33 +01:00
|
|
|
argv[0] = chunk;
|
|
|
|
} else {
|
|
|
|
argv[0] = Null();
|
|
|
|
}
|
|
|
|
|
2009-03-06 19:49:52 +01:00
|
|
|
Handle<Value> result = onbody->Call(js_object, argc, argv);
|
2009-03-03 19:17:33 +01:00
|
|
|
|
2009-03-04 00:31:37 +01:00
|
|
|
if(try_catch.HasCaught())
|
|
|
|
node_fatal_exception(try_catch);
|
2009-03-03 19:17:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Local<Object>
|
|
|
|
HttpRequest::CreateJSObject ()
|
|
|
|
{
|
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
if (request_template.IsEmpty()) {
|
|
|
|
Handle<ObjectTemplate> raw_template = ObjectTemplate::New();
|
|
|
|
raw_template->SetInternalFieldCount(1);
|
|
|
|
raw_template->Set(respond_str, FunctionTemplate::New(RespondCallback));
|
|
|
|
|
|
|
|
request_template = Persistent<ObjectTemplate>::New(raw_template);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an empty http request wrapper.
|
|
|
|
Handle<Object> result = request_template->NewInstance();
|
|
|
|
|
|
|
|
// Wrap the raw C++ pointer in an External so it can be referenced
|
|
|
|
// from within JavaScript.
|
|
|
|
Handle<External> request_ptr = External::New(this);
|
|
|
|
|
|
|
|
// Store the request pointer in the JavaScript wrapper.
|
|
|
|
result->SetInternalField(0, request_ptr);
|
|
|
|
|
|
|
|
result->Set ( path_str
|
|
|
|
, String::New(path.c_str(), path.length())
|
|
|
|
);
|
|
|
|
|
|
|
|
result->Set ( uri_str
|
|
|
|
, String::New(uri.c_str(), uri.length())
|
|
|
|
);
|
|
|
|
|
|
|
|
result->Set ( query_string_str
|
|
|
|
, String::New(query_string.c_str(), query_string.length())
|
|
|
|
);
|
|
|
|
|
|
|
|
result->Set ( fragment_str
|
|
|
|
, String::New(fragment.c_str(), fragment.length())
|
|
|
|
);
|
|
|
|
|
|
|
|
result->Set ( method_str
|
|
|
|
, GetMethodString(parser_info.method)
|
|
|
|
);
|
|
|
|
|
|
|
|
char version[10];
|
|
|
|
snprintf ( version
|
|
|
|
, 10 // big enough? :)
|
|
|
|
, "%d.%d"
|
|
|
|
, parser_info.version_major
|
|
|
|
, parser_info.version_minor
|
|
|
|
);
|
|
|
|
result->Set ( http_version_str
|
|
|
|
, String::New(version)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> headers = Object::New();
|
|
|
|
list<string>::iterator field_iterator = header_fields.begin();
|
|
|
|
list<string>::iterator value_iterator = header_values.begin();
|
|
|
|
while( value_iterator != header_values.end() ) {
|
|
|
|
string &f = *field_iterator;
|
|
|
|
string &v = *value_iterator;
|
|
|
|
|
2009-03-15 09:44:26 +01:00
|
|
|
headers->Set( String::NewSymbol(f.c_str(), f.length())
|
2009-03-03 19:17:33 +01:00
|
|
|
, String::New(v.c_str(), v.length() )
|
|
|
|
);
|
|
|
|
|
|
|
|
field_iterator++;
|
|
|
|
value_iterator++;
|
|
|
|
}
|
2009-03-05 14:45:11 +01:00
|
|
|
result->Set(headers_str, headers);
|
2009-03-03 19:17:33 +01:00
|
|
|
|
|
|
|
js_object = Persistent<Object>::New(result);
|
2009-03-07 17:08:35 +01:00
|
|
|
// XXX does the request's js_object need a MakeWeak callback?
|
|
|
|
// i dont think so because at some point the connection closes
|
|
|
|
// and we're going to delete the request.
|
|
|
|
|
2009-03-03 19:17:33 +01:00
|
|
|
return scope.Close(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static oi_socket*
|
|
|
|
on_connection (oi_server *_server, struct sockaddr *addr, socklen_t len)
|
2009-02-16 01:34:45 +01:00
|
|
|
{
|
2009-03-03 01:56:15 +01:00
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
Server *server = static_cast<Server*> (_server->data);
|
|
|
|
|
|
|
|
Handle<Value> callback_v = server->Callback();
|
|
|
|
|
|
|
|
if(callback_v == Undefined())
|
|
|
|
return NULL;
|
|
|
|
|
2009-02-16 01:34:45 +01:00
|
|
|
Connection *connection = new Connection();
|
|
|
|
|
2009-03-03 01:56:15 +01:00
|
|
|
Handle<Function> f = Handle<Function>::Cast(callback_v);
|
2009-03-06 19:49:52 +01:00
|
|
|
connection->js_onrequest = Persistent<Function>::New(f);
|
2009-02-16 01:34:45 +01:00
|
|
|
|
|
|
|
return &connection->socket;
|
|
|
|
}
|
|
|
|
|
2009-03-06 19:49:52 +01:00
|
|
|
Connection::Connection ()
|
|
|
|
{
|
2009-03-07 17:08:35 +01:00
|
|
|
oi_socket_init (&socket, 30.0); // TODO make timeout adjustable
|
|
|
|
socket.on_read = on_read;
|
|
|
|
socket.on_error = NULL;
|
|
|
|
socket.on_close = on_close;
|
|
|
|
socket.on_timeout = NULL;
|
|
|
|
socket.on_drain = NULL;
|
|
|
|
socket.data = this;
|
2009-03-06 19:49:52 +01:00
|
|
|
|
|
|
|
ebb_request_parser_init (&parser);
|
2009-03-07 17:08:35 +01:00
|
|
|
parser.new_request = on_request;
|
|
|
|
parser.data = this;
|
2009-03-06 19:49:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Connection::~Connection ()
|
|
|
|
{
|
2009-03-07 17:08:35 +01:00
|
|
|
list<HttpRequest*>::iterator it = requests.begin();
|
|
|
|
|
|
|
|
// delete all the requests
|
|
|
|
|
|
|
|
for(it = requests.begin(); it != requests.end(); it++)
|
|
|
|
delete *it;
|
|
|
|
|
|
|
|
for(it = finished_requests.begin(); it != finished_requests.end(); it++)
|
|
|
|
delete *it;
|
2009-03-06 19:49:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Connection::Parse(const void *buf, size_t count)
|
|
|
|
{
|
2009-03-07 17:08:35 +01:00
|
|
|
// FIXME change ebb_request_parser to have void* arg
|
2009-03-06 19:49:52 +01:00
|
|
|
ebb_request_parser_execute ( &parser
|
|
|
|
, static_cast<const char*> (buf)
|
|
|
|
, count
|
|
|
|
);
|
|
|
|
|
|
|
|
if(ebb_request_parser_has_error(&parser)) {
|
|
|
|
fprintf(stderr, "parse error closing connection\n");
|
|
|
|
oi_socket_close(&socket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-07 17:08:35 +01:00
|
|
|
void
|
|
|
|
Connection::AddRequest(HttpRequest *request)
|
2009-03-06 19:49:52 +01:00
|
|
|
{
|
|
|
|
requests.push_back(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Connection::Write ( )
|
|
|
|
{
|
|
|
|
if(requests.size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
HttpRequest *request = requests.front();
|
|
|
|
|
|
|
|
while(request->output.size() > 0) {
|
|
|
|
oi_buf *buf = request->output.front();
|
|
|
|
oi_socket_write(&socket, buf);
|
|
|
|
request->output.pop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(request->done) {
|
|
|
|
if(!ebb_request_should_keep_alive(&request->parser_info)) {
|
|
|
|
socket.on_drain = oi_socket_close;
|
2009-03-07 17:08:35 +01:00
|
|
|
}
|
|
|
|
|
2009-03-06 19:49:52 +01:00
|
|
|
requests.pop_front();
|
2009-03-07 17:08:35 +01:00
|
|
|
finished_requests.push_back(request);
|
|
|
|
|
2009-03-06 19:49:52 +01:00
|
|
|
Write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-07 17:08:35 +01:00
|
|
|
void
|
|
|
|
Connection::Close ( )
|
|
|
|
{
|
|
|
|
oi_socket_close(&socket);
|
|
|
|
}
|
|
|
|
|
2009-03-03 19:17:33 +01:00
|
|
|
static void
|
|
|
|
server_destroy (Persistent<Value> _, void *data)
|
2009-02-16 01:34:45 +01:00
|
|
|
{
|
2009-03-03 01:56:15 +01:00
|
|
|
Server *server = static_cast<Server *> (data);
|
|
|
|
delete server;
|
2009-02-16 01:34:45 +01:00
|
|
|
}
|
|
|
|
|
2009-03-03 01:56:15 +01:00
|
|
|
Server::Server (Handle<Object> _js_server)
|
2009-02-16 01:34:45 +01:00
|
|
|
{
|
2009-03-03 01:56:15 +01:00
|
|
|
oi_server_init(&server, 1024);
|
|
|
|
server.on_connection = on_connection;
|
|
|
|
server.data = this;
|
|
|
|
HandleScope scope;
|
|
|
|
js_server = Persistent<Object>::New (_js_server);
|
|
|
|
// are we ever going to need this external?
|
|
|
|
js_server->SetInternalField (0, External::New(this));
|
|
|
|
js_server.MakeWeak (this, server_destroy);
|
2009-02-16 01:34:45 +01:00
|
|
|
}
|
|
|
|
|
2009-03-03 01:56:15 +01:00
|
|
|
Server::~Server ()
|
2009-02-23 13:48:34 +01:00
|
|
|
{
|
2009-03-03 01:56:15 +01:00
|
|
|
Stop();
|
|
|
|
js_server.Dispose();
|
|
|
|
js_server.Clear(); // necessary?
|
2009-02-23 13:48:34 +01:00
|
|
|
}
|
|
|
|
|
2009-03-03 01:56:15 +01:00
|
|
|
int
|
|
|
|
Server::Start(struct addrinfo *servinfo)
|
2009-02-23 13:48:34 +01:00
|
|
|
{
|
2009-03-03 01:56:15 +01:00
|
|
|
int r = oi_server_listen(&server, servinfo);
|
|
|
|
if(r == 0)
|
2009-03-04 00:31:37 +01:00
|
|
|
oi_server_attach(&server, node_loop());
|
2009-03-03 01:56:15 +01:00
|
|
|
return r;
|
2009-02-23 13:48:34 +01:00
|
|
|
}
|
|
|
|
|
2009-03-03 01:56:15 +01:00
|
|
|
void
|
|
|
|
Server::Stop()
|
2009-02-16 01:34:45 +01:00
|
|
|
{
|
2009-03-03 01:56:15 +01:00
|
|
|
oi_server_close (&server);
|
|
|
|
oi_server_detach (&server);
|
|
|
|
}
|
2009-03-02 15:49:24 +01:00
|
|
|
|
2009-03-03 01:56:15 +01:00
|
|
|
/* This constructor takes 2 arguments: host, port. */
|
|
|
|
static Handle<Value>
|
2009-03-06 19:49:52 +01:00
|
|
|
newHTTPServer (const Arguments& args)
|
2009-03-03 01:56:15 +01:00
|
|
|
{
|
2009-03-09 14:09:09 +01:00
|
|
|
if (args.Length() < 3)
|
2009-03-03 01:56:15 +01:00
|
|
|
return Undefined();
|
2009-03-02 15:49:24 +01:00
|
|
|
|
2009-02-16 01:34:45 +01:00
|
|
|
HandleScope scope;
|
2009-02-23 13:48:34 +01:00
|
|
|
|
2009-03-13 12:22:35 +01:00
|
|
|
char *host = NULL;
|
|
|
|
String::AsciiValue host_v(args[0]->ToString());
|
|
|
|
if(args[0]->IsString()) {
|
|
|
|
host = *host_v;
|
|
|
|
}
|
2009-03-03 01:56:15 +01:00
|
|
|
String::AsciiValue port(args[1]->ToString());
|
2009-02-16 01:34:45 +01:00
|
|
|
|
2009-03-09 14:09:09 +01:00
|
|
|
Handle<Function> onrequest = Handle<Function>::Cast(args[2]);
|
|
|
|
args.This()->Set(on_request_str, onrequest);
|
|
|
|
|
2009-02-16 01:34:45 +01:00
|
|
|
// get addrinfo for localhost, PORT
|
|
|
|
struct addrinfo *servinfo;
|
|
|
|
struct addrinfo hints;
|
|
|
|
memset(&hints, 0, sizeof hints);
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
2009-03-09 14:09:09 +01:00
|
|
|
// FIXME BLOCKING
|
2009-03-13 12:22:35 +01:00
|
|
|
int r = getaddrinfo(host, *port, &hints, &servinfo);
|
2009-03-03 01:56:15 +01:00
|
|
|
if (r != 0)
|
|
|
|
return Undefined(); // XXX raise error?
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// TODO host is ignored for now assumed localhost
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
Server *server = new Server(args.This());
|
|
|
|
if(server == NULL)
|
|
|
|
return Undefined(); // XXX raise error?
|
|
|
|
|
|
|
|
r = server->Start(servinfo);
|
|
|
|
if (r != 0)
|
|
|
|
return Undefined(); // XXX raise error?
|
|
|
|
|
|
|
|
return args.This();
|
|
|
|
}
|
2009-02-16 01:34:45 +01:00
|
|
|
|
2009-03-04 00:31:37 +01:00
|
|
|
void
|
2009-03-04 10:08:33 +01:00
|
|
|
Init_http (Handle<Object> target)
|
2009-03-03 01:56:15 +01:00
|
|
|
{
|
|
|
|
HandleScope scope;
|
2009-02-16 01:34:45 +01:00
|
|
|
|
2009-03-06 19:49:52 +01:00
|
|
|
Local<FunctionTemplate> server_t = FunctionTemplate::New(newHTTPServer);
|
2009-03-03 01:56:15 +01:00
|
|
|
server_t->InstanceTemplate()->SetInternalFieldCount(1);
|
2009-03-15 11:11:51 +01:00
|
|
|
|
|
|
|
server_t->Set("INVALID_STATE_ERR", Integer::New(INVALID_STATE_ERR));
|
2009-02-23 13:48:34 +01:00
|
|
|
|
2009-03-05 14:35:54 +01:00
|
|
|
target->Set(String::New("HTTPServer"), server_t->GetFunction());
|
2009-03-04 00:31:37 +01:00
|
|
|
|
2009-03-03 02:15:26 +01:00
|
|
|
path_str = Persistent<String>::New( String::NewSymbol("path") );
|
|
|
|
uri_str = Persistent<String>::New( String::NewSymbol("uri") );
|
|
|
|
query_string_str = Persistent<String>::New( String::NewSymbol("query_string") );
|
|
|
|
fragment_str = Persistent<String>::New( String::NewSymbol("fragment") );
|
|
|
|
method_str = Persistent<String>::New( String::NewSymbol("method") );
|
|
|
|
http_version_str = Persistent<String>::New( String::NewSymbol("http_version") );
|
2009-03-05 14:45:11 +01:00
|
|
|
headers_str = Persistent<String>::New( String::NewSymbol("headers") );
|
2009-03-03 02:15:26 +01:00
|
|
|
|
2009-03-06 19:49:52 +01:00
|
|
|
on_request_str = Persistent<String>::New( String::NewSymbol("onrequest") );
|
|
|
|
on_body_str = Persistent<String>::New( String::NewSymbol("onbody") );
|
2009-03-03 02:15:26 +01:00
|
|
|
respond_str = Persistent<String>::New( String::NewSymbol("respond") );
|
|
|
|
|
|
|
|
copy_str = Persistent<String>::New( String::New("COPY") );
|
|
|
|
delete_str = Persistent<String>::New( String::New("DELETE") );
|
|
|
|
get_str = Persistent<String>::New( String::New("GET") );
|
|
|
|
head_str = Persistent<String>::New( String::New("HEAD") );
|
|
|
|
lock_str = Persistent<String>::New( String::New("LOCK") );
|
|
|
|
mkcol_str = Persistent<String>::New( String::New("MKCOL") );
|
|
|
|
move_str = Persistent<String>::New( String::New("MOVE") );
|
|
|
|
options_str = Persistent<String>::New( String::New("OPTIONS") );
|
|
|
|
post_str = Persistent<String>::New( String::New("POST") );
|
|
|
|
propfind_str = Persistent<String>::New( String::New("PROPFIND") );
|
|
|
|
proppatch_str = Persistent<String>::New( String::New("PROPPATCH") );
|
|
|
|
put_str = Persistent<String>::New( String::New("PUT") );
|
|
|
|
trace_str = Persistent<String>::New( String::New("TRACE") );
|
|
|
|
unlock_str = Persistent<String>::New( String::New("UNLOCK") );
|
2009-02-16 01:34:45 +01:00
|
|
|
}
|