Skip to content

Commit

Permalink
With header reading now working CGI handling is almost complete
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw committed Jun 27, 2012
1 parent a36a49d commit 7099667
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 39 deletions.
37 changes: 22 additions & 15 deletions loader/apache.winxed
@@ -1,43 +1,47 @@
namespace apache {
class output {
var writer;
var request;
var buffer;
var contentWriter;
var headerWriter;
var bytesWritten; // content bytes only

function output(var request) {
self.init(request);
}

function init(var request) {
self.request = request;
self.writer = dlfunc(null, "mod_parrot_write", "ipip");
self.contentWriter = dlfunc(null, "mod_parrot_write", "ipip");
self.headerWriter = dlfunc(null, "mod_parrot_header_out", "vPPPp");
self.buffer = new ByteBuffer; // warn then, I dare you
self.bytesWritten = 0;
}

/* i split these functions because winxed does not have an easy way
* to call super() methods. puts() is the entry point for the C
* layer, write() for subclasses */
function puts(string msg) {
self.write(msg);
return self.write(msg);
}

function write(string msg) {
var writer = self.writer;
var writer = self.contentWriter;
self.buffer =: msg;
return writer(self.buffer, elements(self.buffer), self.request);
int b = writer(self.buffer, elements(self.buffer), self.request);
self.bytesWritten += b; // should check if this is larger than 0
return b;
}

function header(string key, string value) {
var writer = self.headerWriter;
writer(getinterp(), key, value, self.request);
}

function headers(var headers) {
for(var k in headers) {
self.header(k, headers[k]);
}
}
}

class input {
var reader;
var contentReader;
var headerReader;
var requestReader;
var setup;
Expand All @@ -46,10 +50,14 @@ namespace apache {
var bytesRead;
var buffer;
var readSize;

function input(var request) {
self.init(request);
}

function init(var request) {
self.request = request;
self.reader = dlfunc(null, "mod_parrot_read", "ipip");
self.contentReader = dlfunc(null, "mod_parrot_read", "ipip");
self.headerReader = dlfunc(null, "mod_parrot_headers_in", "PPp");
self.requestReader = dlfunc(null, "mod_parrot_request_parameters", "PPp");
self.buffer = new ByteBuffer;
Expand All @@ -58,7 +66,6 @@ namespace apache {
self.remaining = setupFunc(request);
self.bytesRead = 0;
}


function readline() {
// until we have a buffering system, do not implement this.
Expand All @@ -76,7 +83,7 @@ namespace apache {

function read(int size) {
self.buffer =: size;
var reader = self.reader;
var reader = self.contentReader;
int readBytes = reader(self.buffer, size, self.request);
if(readBytes > 0) {
self.remaining -= readBytes;
Expand Down
46 changes: 23 additions & 23 deletions loader/cgi.winxed
Expand Up @@ -8,31 +8,31 @@ inline find_not_cclass(int cclass, string s, int startIdx, int length) return in
}
namespace cgi {
class output : apache.output {
var isContent;

function puts(string msg) {
if(self.isContent) {
return self.write(msg);
}
var headers = split("\n", msg);
while(elements(headers) > 0) {
string h = shift_string(headers);
int i = indexof(h, ":");
if(i < 0) { // no more headers, and yes, that means that a
// faulty header causes mp to spit out all
// following headers. though luck
unshift(headers, h);
break;
if(self.bytesWritten == 0) {
var lines = split("\n", msg);
while(elements(lines) > 0) {
string payload = shift_string(lines);
int i = indexof(payload, ':');
if(indexof(payload, "HTTP") == 0) {
// read a status code cleverly
continue;
} else if(i > 0) {
// must be a header
string key = substr(payload, 0, i);
string val = substr(payload, i + 1);
self.header(key, val);
} else { // it is either the end of headers or a
// content line
if(length(payload) > 0)
unshift(lines, payload);
break;
}
}
string key = substr(h, 0, i);
string val = substr(h, i + 1);
self.header(key, val);
}
// if there are any lines left, print them out
if(length(headers) > 0) {
string content = join("\n", headers);
self.isContent = true;
self.write(content);
if(elements(lines) > 0)
self.write(join("\n", lines));
} else {
self.write(msg);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pudding/cgi.pl
Expand Up @@ -3,7 +3,7 @@
use warnings;
use Server;
use Client;
use Test::More tests => 2;
use Test::More tests => 3;
use config;
use Data::Dumper;
my $server = Server->new($config::HTTPD);
Expand Down

0 comments on commit 7099667

Please sign in to comment.