Skip to content

Commit

Permalink
[Net] Add Net to the build and fix errors and warnings. Add more requ…
Browse files Browse the repository at this point in the history
…est handlers to the file protocol
  • Loading branch information
Whiteknight committed Mar 24, 2012
1 parent 49052aa commit 382f1d6
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 23 deletions.
14 changes: 14 additions & 0 deletions setup.winxed
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,20 @@ function setup_experimental_libraries(var rosella)
"genetic/Mutator",
"genetic/mutator/Generic"
);

setup_unstable_lib(rosella, "net", ["Core", "FileSystem", "Date"],
"net/Includes",
"net/Http",
"net/MimeBase64",
"net/Uri",
"net/UserAgent",
"net/http/Header",
"net/http/Message",
"net/http/Protocol",
"net/http/Request",
"net/http/Response",
"net/http/SocketFactory"
);
}

function setup_utilities(var rosella)
Expand Down
2 changes: 1 addition & 1 deletion src/unstable/net/Uri.winxed
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ class Rosella.Net.Uri
{
string uri = self.uri;
int idx = index(uri, "//");
return substring(uri, idx);
return substring(uri, idx + 2);
}
}
4 changes: 2 additions & 2 deletions src/unstable/net/http/Header.winxed
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Rosella.Net.Http.Header

function Header()
{
self.values = values;
self.values = {};
// TODO: Set hash value to be strings to avoid boxing
}

Expand Down Expand Up @@ -37,7 +37,7 @@ class Rosella.Net.Http.Header

function get_header_text(var sb = new 'StringBuilder')
{
foreach (string name in self.values) {
for (string name in self.values) {
push(sb, name);
push(sb, ": ");
push(sb, self.values[name]);
Expand Down
4 changes: 2 additions & 2 deletions src/unstable/net/http/Message.winxed
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Rosella.Net.Http.Message.Factory : Rosella.ObjectFactory
{
var header_type;
function Factory(var type = class Rosella.Http.Message,
var header_type = class Rosella.Http.Header)
function Factory(var type = class Rosella.Net.Http.Message,
var header_type = class Rosella.Net.Http.Header)
{
self.ObjectFactory(type);
self.header_type = header_type;
Expand Down
54 changes: 39 additions & 15 deletions src/unstable/net/http/Protocol.winxed
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ class Rosella.Net.Http.Protocol

function user_agent() { return self.user_agent; }

function __dispatch_request(string name, var request, var proxy)
function __dispatch_request(string name, var request, string path, var proxy)
{
var m = find_method(self, "request_" + name);
if (m == null)
return new Rosella.Net.Response(RC_INTERNAL_SERVER_ERROR, "Cannot dispatch request type '%s' for protocol '%s'", name, self.protocol_name());
self.*m(request, proxy);
return new Rosella.Net.Http.Response(RC_INTERNAL_SERVER_ERROR, "Cannot dispatch request type '%s' for protocol '%s'", name, self.protocol_name());
self.*m(request, path, proxy);
}

function __error_bad_protocol(string protocol)
{
return new Rosella.Net.Response(RC_INTERNAL_SERVER_ERROR, "Bad protocol. Expected '%s' but have '%s'", self.protocol_name(), protocol);
return new Rosella.Net.Http.Response(RC_INTERNAL_SERVER_ERROR, "Bad protocol. Expected '%s' but have '%s'", self.protocol_name(), protocol);
}

function __error_not_found(var uri)
{
return new Rosella.Net.Response(RC_NOT_FOUND, "Could not find resource ", uri.path());
return new Rosella.Net.Http.Response(RC_NOT_FOUND, "Could not find resource ", uri.path());
}
}

Expand All @@ -52,22 +52,46 @@ class Rosella.Net.Http.Protocol.File : Rosella.Net.Http.Protocol
string uri_protocol = uri.protocol();
if (uri_protocol != "file")
return self.__error_bad_protocol(uri_protocol);
return self.__dispatch_method(request.method(), request, proxy);
return self.__dispatch_method(request.method(), request, uri.path(), proxy);
}

function request_HEAD(var request, var protocol)
function request_HEAD(var request, string path, var proxy)
{
return self.request_GET(request, protocol);
var f = new Rosella.FileSystem.File(path);
if (!f.exists())
return __error_not_found(request.uri());
var response = new Rosella.Net.Http.Response(RC_OK);
response.add_header("Last-Modified", f.modify_time().format_string(""));
response.add_header("Content-Length", f.size());
return response;
}

function request_GET(var request, var protocol)
function request_GET(var request, string path, var proxy)
{
var uri = request.uri();
string path = uri.path();
var f = new Rosella.FileSystem.File(path);
if (!f.exists)
return __error_not_found(uri);
if (!f.exists())
return __error_not_found(request.uri());
var response = new Rosella.Net.Http.Response(RC_OK);
response.add_header("Last-Modified", f.modify_time().format_string(""));
response.add_header("Content-Length", f.size());
response.set_content(f.read_all_text());
return response;
}

function request_PUT(var request, string path, var proxy)
{
var f = new Rosella.FileSystem.File(path);
f.write_all_text(request.get_content());
return new Rosella.Net.Http.Response(RC_OK);
}

function request_DELETE(var request, string path, var proxy)
{
var f = new Rosella.FileSystem.File(path);
if (!f.exists())
return __error_not_found(request.uri());
f.delete();
return new Rosella.Net.Http.Response(RC_OK);
}
}

Expand Down Expand Up @@ -115,8 +139,8 @@ class Rosella.Net.Http.Protocol.Factory : Rosella.ObjectFactory
function __get_default_types()
{
return {
"http": class Rosella.Net.Protocol.Http,
"file": class Rosella.Net.Protocol.File
"http": class Rosella.Net.Http.Protocol.Http,
"file": class Rosella.Net.Http.Protocol.File
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/unstable/net/http/Request.winxed
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ class Rosella.Net.Http.Request
var uri;
var proxy;

function Request()
function Request(var header)
{
self.Message(header);
}

function set_proxy(var p)
Expand Down
4 changes: 2 additions & 2 deletions src/unstable/net/http/Response.winxed
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Rosella.Net.Http.Response
class Rosella.Net.Http.Response : Rosella.Net.Http.Message
{
var code;
var message;
var previous;
var request;

function Response(int code, string message, var previous = null, var request = null)
function Response(int code, string message = "", var previous = null, var request = null)
{
self.code = code;
self.message = message;
Expand Down

0 comments on commit 382f1d6

Please sign in to comment.