Skip to content

Commit

Permalink
Moved utilities to mod_parrot_util, added request and input header re…
Browse files Browse the repository at this point in the history
…trieval to input
  • Loading branch information
bdw committed Jun 23, 2012
1 parent 3ca8758 commit e1981b5
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 71 deletions.
1 change: 1 addition & 0 deletions Configure.pl
Expand Up @@ -58,6 +58,7 @@ sub write_definitions {
HTTPD => $httpd,
FLAGS => qx/parrot_config embed-ldflags/ . ' ' . qx/parrot_config embed-cflags/,
);

chomp $make{$_} for (keys(%make));

$make{FLAGS} =~ s/(-[lLI])(\S+)/$1 $2/g;
Expand Down
21 changes: 18 additions & 3 deletions loader/apache.winxed
Expand Up @@ -32,15 +32,19 @@ namespace apache {
}

class input {
var func;
var reader;
var headerReader;
var requestReader;
var request;
var remaining;
var buffer;
var readSize;

function input(var request) {
self.request = request;
self.func = dlfunc(null, "mod_parrot_read", "ipip");
self.reader = dlfunc(null, "mod_parrot_read", "ipip");
self.headerReader = dlfunc(null, "mod_parrot_headers_in", "PPp");
self.requestReader = dlfunc(null, "mod_parrot_request_parameters", "PPp");
var setupFunc = dlfunc(null, "mod_parrot_setup_input", "ip");
self.remaining = setupFunc(request);
self.buffer = new ByteBuffer;
Expand All @@ -50,6 +54,7 @@ namespace apache {
function readline() {
// until we have a buffering system, do not implement this.
// it isn't all that sensible, after all

}

function readall() {
Expand All @@ -63,7 +68,7 @@ namespace apache {

function read(int size) {
self.buffer =: size;
var reader = self.func;
var reader = self.reader;
int readBytes = reader(self.buffer, size, self.request);
if(readBytes > 0) {
self.remaining -= readBytes;
Expand All @@ -73,6 +78,16 @@ namespace apache {
return "";
}
}

function headers() {
var reader = self.headerReader;
return reader(getinterp(), self.request);
}

function request() {
var reader = self.requestReader;
return reader(getinterp(), self.request);
}
}

function setup[main](var request) {
Expand Down
12 changes: 9 additions & 3 deletions loader/mod_parrot.winxed
@@ -1,10 +1,16 @@


function main[main](var request) {
say("Hello, world!");
print("1 + 1 = ", 1 + 1);
print("\n");
var request = getstdin().request();
for(string k in request) {
say(sprintf("%s: %s", [k, request[k]]));
}
var headers = getstdin().headers();
for(string k in headers) {
say(sprintf("%s: %s", [k, headers[k]]));
}
say(getstdin().readall());

getstdout().headers({ "X-Foo":"bar", "X-Quix": "quam"});
}

2 changes: 1 addition & 1 deletion module/Makefile
@@ -1,6 +1,6 @@
include ../config.mk

SOURCE=mod_parrot.c mod_parrot_io.c mod_parrot_run.c
SOURCE=mod_parrot.c mod_parrot_io.c mod_parrot_run.c mod_parrot_util.c
MODULE=mod_parrot.la
OUTPUT=../build/mod_parrot.so

Expand Down
3 changes: 2 additions & 1 deletion module/mod_parrot.h
Expand Up @@ -25,4 +25,5 @@ int mod_parrot_read(void * buf, size_t size, request_rec * req);
Parrot_PMC mod_parrot_interpreter(mod_parrot_conf * conf);
int mod_parrot_run(Parrot_PMC interp, request_rec *req);
void mod_parrot_setup_args(Parrot_PMC interp, request_rec *req, Parrot_PMC *args);

Parrot_PMC mod_parrot_new_hash(Parrot_PMC interp);
void mod_parrot_hash_set(Parrot_PMC interp, Parrot_PMC hash, char * key, char * value);
56 changes: 19 additions & 37 deletions module/mod_parrot_io.c
Expand Up @@ -2,26 +2,6 @@
#include <strings.h>
#include <ctype.h>

/* The static functions below should be in some mod_parrot_util file */
static Parrot_PMC new_instance(Parrot_PMC i, char * class, Parrot_PMC initPMC) {
Parrot_PMC classPMC, keyPMC;
Parrot_String className;
Parrot_PMC instancePMC;
Parrot_api_string_import_ascii(i, class, &className);
Parrot_api_pmc_box_string(i, className, &keyPMC);
Parrot_api_pmc_get_class(i, keyPMC, &classPMC);
Parrot_api_pmc_new_from_class(i, classPMC, initPMC, &instancePMC);
return instancePMC;
}

static void hash_set(Parrot_PMC i, Parrot_PMC h, char * k, char * v) {
Parrot_String kS, vS; // key string, value string
Parrot_PMC vP;
Parrot_api_string_import_ascii(i, k, &kS);
Parrot_api_string_import_ascii(i, v, &vS);
Parrot_api_pmc_box_string(i, vS, &vP);
Parrot_api_pmc_set_keyed_string(i, h, kS, vP);
}

static char * header_convert(apr_pool_t *pool, char * header) {
int idx;
Expand Down Expand Up @@ -55,29 +35,29 @@ static char * ipaddr(apr_sockaddr_t *a) {
Parrot_PMC mod_parrot_request_parameters(Parrot_PMC interp, request_rec * req) {
Parrot_PMC hash;
/* todo, make this non-nasty. although it works */
hash = new_instance(interp, "Hash", NULL);
hash = mod_parrot_new_hash(interp);

hash_set(interp, hash, "REQUEST_METHOD", (char*)req->method);
hash_set(interp, hash, "REQUEST_URI", req->unparsed_uri);
hash_set(interp, hash, "QUERY_STRING", req->args ? req->args : "");
hash_set(interp, hash, "HTTP_HOST", (char*)req->hostname);
hash_set(interp, hash, "SCRIPT_NAME", req->filename);
hash_set(interp, hash, "PATH_INFO", req->path_info);
hash_set(interp, hash, "SERVER_NAME", req->server->server_hostname);
hash_set(interp, hash, "SERVER_PROTOCOL", req->protocol);
mod_parrot_hash_set(interp, hash, "REQUEST_METHOD", (char*)req->method);
mod_parrot_hash_set(interp, hash, "REQUEST_URI", req->unparsed_uri);
mod_parrot_hash_set(interp, hash, "QUERY_STRING", req->args ? req->args : "");
mod_parrot_hash_set(interp, hash, "HTTP_HOST", (char*)req->hostname);
mod_parrot_hash_set(interp, hash, "SCRIPT_NAME", req->filename);
mod_parrot_hash_set(interp, hash, "PATH_INFO", req->path_info);
mod_parrot_hash_set(interp, hash, "SERVER_NAME", req->server->server_hostname);
mod_parrot_hash_set(interp, hash, "SERVER_PROTOCOL", req->protocol);

/* Network parameters. This should be simpler, but it isn't. */
hash_set(interp, hash, "SERVER_ADDR", ipaddr(req->connection->local_addr));
hash_set(interp, hash, "SERVER_PORT",
mod_parrot_hash_set(interp, hash, "SERVER_ADDR", ipaddr(req->connection->local_addr));
mod_parrot_hash_set(interp, hash, "SERVER_PORT",
apr_itoa(req->pool, req->connection->local_addr->port));

hash_set(interp, hash, "REMOTE_ADDR",
mod_parrot_hash_set(interp, hash, "REMOTE_ADDR",
ipaddr(req->connection->remote_addr));
hash_set(interp, hash, "REMOTE_PORT",
mod_parrot_hash_set(interp, hash, "REMOTE_PORT",
apr_itoa(req->pool, req->connection->remote_addr->port));

if(req->server->server_admin) /* I don't believe this is ever NULL */
hash_set(interp, hash, "SERVER_ADMIN", req->server->server_admin);
mod_parrot_hash_set(interp, hash, "SERVER_ADMIN", req->server->server_admin);
return hash;
}

Expand All @@ -92,15 +72,17 @@ Parrot_PMC mod_parrot_headers_in(Parrot_PMC interp, request_rec * req) {
const apr_array_header_t *array;
apr_table_entry_t * entries;
Parrot_PMC hash;
char * key;
int idx;
hash = new_instance(interp, "HASH", NULL);
hash = mod_parrot_new_hash(interp);
array = apr_table_elts(req->headers_in);
entries = (apr_table_entry_t *) array->elts;
for(idx = 0; idx < array->nelts; idx++) {
if(!strcasecmp(entries[idx].key, "host"))
continue;
hash_set(interp, hash, header_convert(req->pool, entries[idx].key),
entries[idx].val);
// should i put this here? or in winxed? i don't know how to do string conversion in b
key = header_convert(req->pool, entries[idx].key);
mod_parrot_hash_set(interp, hash, key, entries[idx].val);
}
return hash;
}
Expand Down
36 changes: 10 additions & 26 deletions module/mod_parrot_run.c
@@ -1,43 +1,26 @@
#include "mod_parrot.h"

/* The static functions below should be in some mod_parrot_util file */
static Parrot_PMC new_instance(Parrot_PMC i, char * class, Parrot_PMC initPMC) {
Parrot_PMC classPMC, keyPMC;
Parrot_String className;
Parrot_PMC instancePMC;
Parrot_api_string_import_ascii(i, class, &className);
Parrot_api_pmc_box_string(i, className, &keyPMC);
Parrot_api_pmc_get_class(i, keyPMC, &classPMC);
Parrot_api_pmc_new_from_class(i, classPMC, initPMC, &instancePMC);
return instancePMC;
}

static void hash_set(Parrot_PMC i, Parrot_PMC h, char * k, char * v) {
Parrot_String kS, vS; // key string, value string
Parrot_PMC vP;
Parrot_api_string_import_ascii(i, k, &kS);
Parrot_api_string_import_ascii(i, v, &vS);
Parrot_api_pmc_box_string(i, vS, &vP);
Parrot_api_pmc_set_keyed_string(i, h, kS, vP);
}


Parrot_PMC mod_parrot_interpreter(mod_parrot_conf * conf) {
Parrot_PMC interp, configHash;
Parrot_PMC pir, pasm;
Parrot_api_make_interpreter(NULL, 0, NULL, &interp);
configHash = new_instance(interp, "Hash", NULL);
hash_set(interp, configHash, "build_dir", BUILDDIR);
hash_set(interp, configHash, "versiondir", VERSIONDIR);
hash_set(interp, configHash, "libdir", LIBDIR);
/* this is to help parrot set up the right paths by itself,
* and yes, i do agree this is a bit of unneccesesary magic.
* Parrots, it appears, are magical birds after all. */
configHash = mod_parrot_new_hash(interp);
mod_parrot_hash_set(interp, configHash, "build_dir", BUILDDIR);
mod_parrot_hash_set(interp, configHash, "versiondir", VERSIONDIR);
mod_parrot_hash_set(interp, configHash, "libdir", LIBDIR);
Parrot_api_set_configuration_hash(interp, configHash);
/* no pir without these calls ;-) */
imcc_get_pir_compreg_api(interp, 1, &pir);
imcc_get_pasm_compreg_api(interp, 1, &pasm);
return interp;
}

extern module mod_parrot;

/* this madness will be simplified in due time */
static Parrot_PMC load_bytecode(Parrot_PMC interp, request_rec *req, char * filename)
{
Parrot_PMC bytecodePMC;
Expand All @@ -62,6 +45,7 @@ int mod_parrot_run(Parrot_PMC interp, request_rec *req) {
if(!Parrot_api_run_bytecode(interp, libraryPMC, requestPMC)) {
return mod_parrot_report_error(interp, req);
}
/* TODO: build a more useful call signature than (request) for loaders */
if(Parrot_api_run_bytecode(interp, bytecodePMC, requestPMC)) {
return OK;
} else {
Expand Down
23 changes: 23 additions & 0 deletions module/mod_parrot_util.c
@@ -0,0 +1,23 @@
#include "mod_parrot.h"
#define HASH_CLASS_NAME "Hash"


Parrot_PMC mod_parrot_new_hash(Parrot_PMC interp) {
Parrot_PMC classKey, classObj, hashObj;
Parrot_String hashName;
Parrot_api_string_import_ascii(interp, HASH_CLASS_NAME, &hashName);
Parrot_api_pmc_box_string(interp, hashName, &classKey);
Parrot_api_pmc_get_class(interp, classKey, &classObj);
if(Parrot_api_pmc_new_from_class(interp, classObj, NULL, &hashObj))
return hashObj;
return NULL;
}

void mod_parrot_hash_set(Parrot_PMC interp, Parrot_PMC hash, char * key, char * value) {
Parrot_String keyString, valueString;
Parrot_PMC valueObj;
Parrot_api_string_import_ascii(interp, key, &keyString);
Parrot_api_string_import_ascii(interp, value, &valueString);
Parrot_api_pmc_box_string(interp, valueString, &valueObj);
Parrot_api_pmc_set_keyed_string(interp, hash, keyString, valueObj);
}

0 comments on commit e1981b5

Please sign in to comment.