Permalink
Browse files

Cherry-pick string-type related parts of the previously reverted commit

  • Loading branch information...
1 parent 38c1a6d commit f9e611f273a0d47f973f5935b596a7f0c0d7d36c @jonashaag committed Feb 19, 2011
Showing with 29 additions and 39 deletions.
  1. +3 −2 bjoern/bjoernmodule.c
  2. +2 −0 bjoern/common.h
  3. +21 −25 bjoern/request.c
  4. +3 −4 bjoern/request.h
  5. +0 −7 bjoern/wsgi.c
  6. +0 −1 bjoern/wsgi.h
View
@@ -73,12 +73,13 @@ run(PyObject* self, PyObject* args)
static PyMethodDef Bjoern_FunctionTable[] = {
{"run", run, METH_VARARGS, run_doc},
{"listen", listen, METH_VARARGS, listen_doc},
- {NULL, NULL, 0, NULL}
+ {NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC initbjoern()
{
- _initialize_wsgi_module();
+ PyType_Ready(&StartResponse_Type);
+ assert(StartResponse_Type.tp_flags & Py_TPFLAGS_READY);
_initialize_static_strings();
PyObject* bjoern_module = Py_InitModule("bjoern", Bjoern_FunctionTable);
View
@@ -7,6 +7,8 @@
#include <stdbool.h>
#include <string.h>
+typedef struct { char* data; size_t len; } string;
+
enum http_status { HTTP_BAD_REQUEST = 1, HTTP_LENGTH_REQUIRED, HTTP_SERVER_ERROR };
size_t unquote_url_inplace(char* url, size_t len);
View
@@ -3,7 +3,7 @@
#include "request.h"
static inline void PyDict_ReplaceKey(PyObject* dict, PyObject* k1, PyObject* k2);
-static PyObject* wsgi_http_header(Request*, const char*, const size_t);
+static PyObject* wsgi_http_header(string header);
static http_parser_settings parser_settings;
static PyObject* wsgi_base_dict = NULL;
@@ -34,6 +34,7 @@ void Request_reset(Request* request)
{
memset(&request->state, 0, sizeof(Request) - (size_t)&((Request*)NULL)->state);
request->state.response_length_unknown = true;
+ request->parser.body = (string){NULL, 0};
}
void Request_free(Request* request)
@@ -88,11 +89,11 @@ void Request_parse(Request* request, const char* data, const size_t data_len)
* [old header data ] ...stuff... [ new header data ]
* ^-------------- A -------------^--------B--------^
*
- * A = XXX- PARSER->XXX_start
+ * A = XXX- PARSER->XXX.data
* B = len
* A + B = old header start to new header end
*/ \
- do { PARSER->name##_len = (name - PARSER->name##_start) + len; } while(0)
+ do { PARSER->name.len = (name - PARSER->name.data) + len; } while(0)
#define _set_header(k, v) PyDict_SetItem(REQUEST->headers, k, v);
#define _set_header_free_value(k, v) \
@@ -113,10 +114,8 @@ void Request_parse(Request* request, const char* data, const size_t data_len)
static int on_message_begin(http_parser* parser)
{
REQUEST->headers = PyDict_New();
- PARSER->field_start = NULL;
- PARSER->field_len = 0;
- PARSER->value_start = NULL;
- PARSER->value_len = 0;
+ PARSER->field = (string){NULL, 0};
+ PARSER->value = (string){NULL, 0};
return 0;
}
@@ -136,43 +135,40 @@ static int on_query_string(http_parser* parser, const char* query, size_t len)
static int on_header_field(http_parser* parser, const char* field, size_t len)
{
- if(PARSER->value_start) {
+ if(PARSER->value.data) {
/* Store previous header and start a new one */
_set_header_free_both(
- wsgi_http_header(REQUEST, PARSER->field_start, PARSER->field_len),
- PyString_FromStringAndSize(PARSER->value_start, PARSER->value_len)
+ wsgi_http_header(PARSER->field),
+ PyString_FromStringAndSize(PARSER->value.data, PARSER->value.len)
);
- } else if(PARSER->field_start) {
+ } else if(PARSER->field.data) {
UPDATE_LENGTH(field);
return 0;
}
- PARSER->field_start = field;
- PARSER->field_len = len;
- PARSER->value_start = NULL;
- PARSER->value_len = 0;
+ PARSER->field = (string){(char*)field, len};
+ PARSER->value = (string){NULL, 0};
return 0;
}
static int
on_header_value(http_parser* parser, const char* value, size_t len)
{
- if(PARSER->value_start) {
+ if(PARSER->value.data) {
UPDATE_LENGTH(value);
} else {
/* Start a new value */
- PARSER->value_start = value;
- PARSER->value_len = len;
+ PARSER->value = (string){(char*)value, len};
}
return 0;
}
static int
on_headers_complete(http_parser* parser)
{
- if(PARSER->field_start) {
+ if(PARSER->field.data) {
_set_header_free_both(
- wsgi_http_header(REQUEST, PARSER->field_start, PARSER->field_len),
- PyString_FromStringAndSize(PARSER->value_start, PARSER->value_len)
+ wsgi_http_header(PARSER->field),
+ PyString_FromStringAndSize(PARSER->value.data, PARSER->value.len)
);
}
return 0;
@@ -242,9 +238,9 @@ on_message_complete(http_parser* parser)
static PyObject*
-wsgi_http_header(Request* request, const char* data, size_t len)
+wsgi_http_header(string header)
{
- PyObject* obj = PyString_FromStringAndSize(NULL, len+strlen("HTTP_"));
+ PyObject* obj = PyString_FromStringAndSize(NULL, header.len+strlen("HTTP_"));
char* dest = PyString_AS_STRING(obj);
*dest++ = 'H';
@@ -253,8 +249,8 @@ wsgi_http_header(Request* request, const char* data, size_t len)
*dest++ = 'P';
*dest++ = '_';
- while(len--) {
- char c = *data++;
+ while(header.len--) {
+ char c = *header.data++;
if(c == '-')
*dest++ = '_';
else if(c >= 'a' && c <= 'z')
View
@@ -19,10 +19,9 @@ typedef struct {
typedef struct {
http_parser parser;
- const char* field_start;
- size_t field_len;
- const char* value_start;
- size_t value_len;
+ string field;
+ string value;
+ string body;
} bj_parser;
typedef struct {
View
@@ -20,7 +20,6 @@ typedef struct {
bool
wsgi_call_application(Request* request)
{
- assert(StartResponse_Type.tp_flags & Py_TPFLAGS_READY);
StartResponse* start_response = PyObject_NEW(StartResponse, &StartResponse_Type);
start_response->request = request;
@@ -379,9 +378,3 @@ wrap_http_chunk_cruft_around(PyObject* chunk)
assert(new_chunk_p == PyString_AS_STRING(new_chunk) + n + chunklen + 1);
return new_chunk;
}
-
-void _initialize_wsgi_module() {
- int ready = PyType_Ready(&StartResponse_Type);
- assert(ready == 0);
- assert(StartResponse_Type.tp_flags & Py_TPFLAGS_READY);
-}
View
@@ -1,7 +1,6 @@
#include <Python.h>
#include "request.h"
-void _initialize_wsgi_module();
bool wsgi_call_application(Request*);
PyObject* wsgi_iterable_get_next_chunk(Request*);
PyObject* wrap_http_chunk_cruft_around(PyObject* chunk);

0 comments on commit f9e611f

Please sign in to comment.