-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add http-parser dependency and bind HttpParser.parseUrl()
- Loading branch information
1 parent
6bcfe7e
commit 7cd757a
Showing
7 changed files
with
148 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule http-parser
added at
8bec3e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "candor.h" | ||
#include "http_parser.h" | ||
#include "lhttp_parser.h" | ||
#include <assert.h> | ||
|
||
static Value* ParseUrl(uint32_t argc, Arguments& argv) { | ||
assert(argc >= 1 && argv[0]->Is<String>()); | ||
String* str = argv[0]->ToString(); | ||
size_t buflen = str->Length(); | ||
const char* buf = str->Value(); | ||
int is_connect = 0; | ||
if (argc > 1 && argv[1]->Is<Boolean>()) { | ||
is_connect = argv[1]->ToBoolean()->IsTrue() ? 1 : 0; | ||
} | ||
http_parser_url u; | ||
int status = http_parser_parse_url(buf, buflen, is_connect, &u); | ||
if (status) { | ||
return Number::NewIntegral(status); | ||
} | ||
|
||
Object* url = Object::New(); | ||
if (u.field_set & (1 << UF_SCHEMA)) { | ||
url->Set("schema", String::New(buf + u.field_data[UF_SCHEMA].off, u.field_data[UF_SCHEMA].len)); | ||
} | ||
if (u.field_set & (1 << UF_HOST)) { | ||
url->Set("host", String::New(buf + u.field_data[UF_HOST].off, u.field_data[UF_HOST].len)); | ||
} | ||
if (u.field_set & (1 << UF_PORT)) { | ||
url->Set("port_string", String::New(buf + u.field_data[UF_PORT].off, u.field_data[UF_PORT].len)); | ||
url->Set("port", Number::NewIntegral(u.port)); | ||
} | ||
if (u.field_set & (1 << UF_PATH)) { | ||
url->Set("path", String::New(buf + u.field_data[UF_PATH].off, u.field_data[UF_PATH].len)); | ||
} | ||
if (u.field_set & (1 << UF_QUERY)) { | ||
url->Set("query", String::New(buf + u.field_data[UF_QUERY].off, u.field_data[UF_QUERY].len)); | ||
} | ||
if (u.field_set & (1 << UF_FRAGMENT)) { | ||
url->Set("fragment", String::New(buf + u.field_data[UF_FRAGMENT].off, u.field_data[UF_FRAGMENT].len)); | ||
} | ||
|
||
return url; | ||
} | ||
|
||
void lhttp_parser_init(Object* global) { | ||
|
||
Object* http_parser = Object::New(); | ||
global->Set("HttpParser", http_parser); | ||
http_parser->Set("parseUrl", Function::New(ParseUrl)); | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#ifndef LHTTP_PARSER_H | ||
#define LHTTP_PARSER_H | ||
|
||
#include "candor.h" | ||
|
||
using namespace candor; | ||
|
||
void lhttp_parser_init(Object* global); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
print = global.print | ||
|
||
// Simple pretty printer | ||
dump = (value) { | ||
type = typeof value | ||
if (type == "nil") { | ||
return "nil" | ||
} | ||
if (type == "boolean" || type == "number") { | ||
return "" + value | ||
} | ||
if (type == "string") { | ||
return '"' + value + '"' | ||
} | ||
if (type == "object") { | ||
keys = keysof value | ||
length = sizeof keys | ||
i = 0 | ||
escaped = "{ " | ||
while (i < length) { | ||
key = keys[i] | ||
part = dump(value[key]) | ||
escaped = escaped + key + ": " + part | ||
if (i < length - 1) { | ||
escaped = escaped + ", " | ||
} | ||
i++ | ||
} | ||
return escaped + " }" | ||
} | ||
if (type == "array") { | ||
length = sizeof value | ||
i = 0 | ||
escaped = "[ " | ||
while (i < length) { | ||
escaped = escaped + dump(value[i]) | ||
print(escaped) | ||
if (i < length - 1) { | ||
escaped = escaped + ", " | ||
} | ||
i++ | ||
} | ||
return escaped + " ]" | ||
} | ||
return "[" + type + "]" | ||
} | ||
|
||
// Quick and dirty pretty printer | ||
p = (label, value) { | ||
print(label, dump(value)) | ||
} | ||
|
||
throw = (err) { | ||
print(err.name + ": " + err.error) | ||
global.exit() | ||
} | ||
|
||
// Used to emit on errors when libuv badness happens | ||
check = (status) { | ||
if (status) { | ||
throw(global.uv.lastError()) | ||
} | ||
} | ||
|
||
|
||
url = "http://candor.io:8080/foo/bar/baz.zip?magic=yes#instant" | ||
p("parsed", global.HttpParser.parseUrl(url)) |