Klaus Demo bjoern / 9133488
Whoops, I forgot common.{c,h} Jonas Haag 9 years ago
2 changed file(s) with 55 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
0 #include "common.h"
1
2 #define UNHEX(c) ((c >= '0' && c <= '9') ? (c - '0') : \
3 (c >= 'a' && c <= 'f') ? (c - 'a' + 10) : \
4 (c >= 'A' && c <= 'F') ? (c - 'A' + 10) : NOHEX)
5 #define NOHEX -1
6
7 size_t unquote_url(const char* url, const size_t len, char* buf) {
8 size_t n = 0;
9 for(const char* end = url+len; url != end; ++buf, ++url, ++n) {
10 if(*url != '%') {
11 /* Not a hex sequeunce */
12 *buf = *url;
13 continue;
14 }
15 if(url >= end-2) {
16 /* Less than two characters left after the '%' */
17 return 0;
18 }
19 char a = UNHEX(url[1]);
20 char b = UNHEX(url[2]);
21 if(a == NOHEX || b == NOHEX) return 0;
22 *buf = a*16 + b;
23 url += 2;
24 assert(url <= end);
25 }
26 return n;
27 }
28
29 /* Case insensitive string comparison */
30 bool string_iequal(const char* a, const size_t len, const char* b)
31 {
32 if(len != strlen(b))
33 return false;
34 for(size_t i=0; i<len; ++i)
35 if(a[i] != b[i] && a[i] - ('a'-'A') != b[i])
36 return false;
37 return true;
38 }
39
40 void _initialize_static_strings() {
41 #define _(name) _##name = PyString_FromString(#name)
42 _(REMOTE_ADDR); _(PATH_INFO); _(QUERY_STRING); _(REQUEST_URI);
43 _(HTTP_FRAGMENT); _(REQUEST_METHOD); _(SERVER_PROTOCOL); _(GET);
44 _(close); _(0); _(Connection);
45 _Content_Length = PyString_FromString("Content-Length");
46 _Content_Type = PyString_FromString("Content-Type");
47 _HTTP_1_1 = PyString_FromString("HTTP/1.1");
48 _HTTP_1_0 = PyString_FromString("HTTP/1.0");
49 _wsgi_input = PyString_FromString("wsgi.input");
50 _empty_string = PyString_FromString("");
51 #undef _
52 }
77 #include <string.h>
88
99 void _initialize_static_strings();
10 bool string_iequal(const char* a, size_t len, const char* b);
10 size_t unquote_url(const char* url, const size_t len, char* buf);
11 bool string_iequal(const char* a, const size_t len, const char* b);
1112
1213 typedef enum {
1314 HTTP_BAD_REQUEST = 1, HTTP_LENGTH_REQUIRED, HTTP_SERVER_ERROR