예제 #1
0
int main(int argc, char **args){
    char* SENDER = NULL;

    if (argc > 1 ) {
        SENDER = args[1];
    }
    printf("Handler ID : %s\n", SENDER);
    bstring pull_addr = bfromcstr("tcp://127.0.0.1:9999");
    bstring pub_addr  = bfromcstr("tcp://127.0.0.1:9998");

    mongrel2_ctx *ctx = mongrel2_init(1); // Yes for threads?

    mongrel2_socket *pull_socket = mongrel2_pull_socket(ctx);
    mongrel2_connect(pull_socket, bdata(pull_addr));

    mongrel2_socket *pub_socket = mongrel2_pub_socket(ctx);
    mongrel2_connect(pub_socket, bdata(pub_addr));
    if (SENDER != NULL) {
        mongrel2_set_identity(pub_socket, SENDER);
    }

    const bstring headers = bfromcstr("HTTP/1.1 200 OK\r\nDate: Fri, 07 Jan 2011 01:15:42 GMT\r\nStatus: 200 OK\r\nConnection: close");
    mongrel2_request *request;

    while(1){
        request = mongrel2_recv(pull_socket);
        btoupper(request->body);
        mongrel2_reply_http(pub_socket, request, headers, request->body);
        mongrel2_disconnect(pub_socket, request);
        mongrel2_request_finalize(request);
    }

    bdestroy(headers);

    bdestroy(pull_addr);
    bdestroy(pub_addr);

    mongrel2_close(pull_socket);
    mongrel2_close(pub_socket);
    mongrel2_deinit(ctx);
    return 0;
}
예제 #2
0
void pp_lua_handle(struct pp_state* state, void* scanner, bstring name, list_t* parameters)
{
	struct lua_preproc* pp;
	struct customarg_entry* carg;
	char* cstr;
	bstring dot;
	unsigned int i;
	int paramtbl;

	// Convert the name to lowercase.
	btolower(name);
	cstr = bstr2cstr(name, '0');

	// Loop through all of the modules.
	list_iterator_start(&modules);
	while (list_iterator_hasnext(&modules))
	{
		pp = list_iterator_next(&modules);

		// Set stack top (I don't know why the top of the
		// stack is negative!)
		lua_settop(pp->state, 0);

		// Search handler table for entries.
		lua_getglobal(pp->state, HANDLER_TABLE_NAME);
		lua_getfield(pp->state, -1, cstr);
		if (!lua_istable(pp->state, -1))
		{
			// No such entry.
			lua_pop(pp->state, 2);
			continue;
		}

		// Call the handler function.
		lua_getfield(pp->state, -1, HANDLER_FIELD_FUNCTION_NAME);
		pp_lua_push_state(pp, state, scanner);
		lua_newtable(pp->state);
		paramtbl = lua_gettop(pp->state);
		for (i = 0; i < list_size(parameters); i++)
		{
			carg = list_get_at(parameters, i);

			lua_newtable(pp->state);
			if (carg->expr != NULL)
				lua_pushstring(pp->state, "EXPRESSION");
			else if (carg->string != NULL)
				lua_pushstring(pp->state, "STRING");
			else if (carg->word != NULL)
				lua_pushstring(pp->state, "WORD");
			else
				lua_pushstring(pp->state, "NUMBER");
			lua_setfield(pp->state, -2, "type");
			if (carg->expr != NULL)
				luaX_pushexpression(pp->state, carg->expr);
			else if (carg->string != NULL)
				lua_pushstring(pp->state, carg->string->data);
			else if (carg->word != NULL)
				lua_pushstring(pp->state, carg->word->data);
			else
				lua_pushnumber(pp->state, carg->number);
			lua_setfield(pp->state, -2, "value");
			lua_rawseti(pp->state, paramtbl, i + 1);
		}
		if (lua_pcall(pp->state, 2, 0, 0) != 0)
		{
			printd(LEVEL_ERROR, "error: unable to call preprocessor handler for '%s'.\n", name->data);
			printd(LEVEL_ERROR, "%s\n", lua_tostring(pp->state, -1));
			bdestroy(name);
			bcstrfree(cstr);
			lua_pop(pp->state, 2);
			list_iterator_stop(&modules);
			list_destroy(parameters);
			return;
		}

		bdestroy(name);
		bcstrfree(cstr);
		lua_pop(pp->state, 2);
		list_iterator_stop(&modules);
		list_destroy(parameters);
		return;
	}
	list_iterator_stop(&modules);

	// There is no custom preprocessor module that handles this directive, however
	// it could be a directive that is recognised by the underlying assembler / compiler,
	// so pass it through as output.
	dot = bfromcstr(".");
	bconcat(dot, name);
	btoupper(dot);
	bconchar(dot, ' ');
	list_iterator_start(parameters);
	while (list_iterator_hasnext(parameters))
	{
		carg = list_iterator_next(parameters);

		// Output the parameter based on the type.
		if (carg->word != NULL)
			bconcat(dot, carg->word);
		else if (carg->string != NULL)
		{
			bconchar(dot, '"');
			bescape(carg->string);
			bconcat(dot, carg->string);
			bconchar(dot, '"');
		}
		else
			bformata(dot, "%i", carg->number);
	}
	list_iterator_stop(parameters);
	handle_pp_lua_print_line(dot->data, scanner);

	// Clean up.
	list_destroy(parameters);
	bdestroy(name);
	bcstrfree(cstr);
}