src/server/safs/proxy.c

changeset 669
ccdc97fd8204
parent 667
1f0f014c0121
child 670
73987de73246
equal deleted inserted replaced
668:0a7d1e9ca6b8 669:ccdc97fd8204
30 30
31 #include "../util/pblock.h" 31 #include "../util/pblock.h"
32 32
33 #include "../proxy/httpclient.h" 33 #include "../proxy/httpclient.h"
34 34
35 static cxstring get_uri_from_clfreq(const char *clfreq) {
36 cxstring uri = { NULL, 0 };
37
38 const char *begin = NULL;
39 const char *str = clfreq;
40 for(;*str != '\0';str++) {
41 if(*str < 33) {
42 if(begin) {
43 str++;
44 break;
45 }
46 } else {
47 if(!begin) {
48 begin = str;
49 }
50 }
51 }
52
53 begin = NULL;
54 for(;*str != '\0';str++) {
55 if(*str > 32) {
56 if(!begin) {
57 begin = str;
58 }
59 } else {
60 if(begin) {
61 break;
62 }
63 }
64 }
65
66 if(begin && *str != '\0') {
67 return cx_strn(begin, str-begin);
68 }
69
70 return uri;
71
72 }
73
35 int http_reverse_proxy_service(pblock *param, Session *sn, Request *rq) { 74 int http_reverse_proxy_service(pblock *param, Session *sn, Request *rq) {
36 EventHandler *ev = sn->ev; 75 EventHandler *ev = sn->ev;
76 const char *method = pblock_findkeyval(pb_key_method, rq->reqpb);
77 const char *clfreq = pblock_findkeyval(pb_key_clf_request, rq->reqpb);
37 78
79 cxstring uri = get_uri_from_clfreq(clfreq);
80 if(uri.length == 0) {
81 return REQ_ABORTED;
82 }
83
84 // setup HttpClient
38 HttpClient *client = http_client_new(ev); 85 HttpClient *client = http_client_new(ev);
86 if(!client) {
87 return REQ_ABORTED;
88 }
39 89
90 if(http_client_set_method(client, method)) {
91 http_client_free(client);
92 return REQ_ABORTED;
93 }
40 94
95 if(http_client_set_uri_len(client, uri.ptr, uri.length)) {
96 http_client_free(client);
97 return REQ_ABORTED;
98 }
99
100 // add request headers to the client
101 CxIterator i = pblock_iterator(rq->headers);
102 cx_foreach(pb_entry*, entry, i) {
103 // TODO: don't pass all headers
104 if(http_client_add_request_header(client, cx_mutstr(entry->param->name), cx_mutstr(entry->param->value))) {
105 http_client_free(client);
106 return REQ_ABORTED;
107 }
108 }
109
110 if(http_client_start(client)) {
111 http_client_free(client);
112 return REQ_ABORTED;
113 }
41 114
42 return REQ_PROCESSING; 115 return REQ_PROCESSING;
43 } 116 }
117
118
119
120 static CX_TEST(test_safs_proxy_get_uri_from_clfreq) {
121 CX_TEST_DO {
122 cxstring ret;
123
124 ret = get_uri_from_clfreq("GET /uri HTTP/1.1");
125 CX_TEST_ASSERT(!cx_strcmp(ret, "/uri"));
126 ret = get_uri_from_clfreq("GET / HTTP/1.1");
127 CX_TEST_ASSERT(!cx_strcmp(ret, "/"));
128 ret = get_uri_from_clfreq("GET /test%20/path HTTP/1.1");
129 CX_TEST_ASSERT(!cx_strcmp(ret, "/test%20/path"));
130 ret = get_uri_from_clfreq(" GET /leading_space HTTP/1.1");
131 CX_TEST_ASSERT(!cx_strcmp(ret, "/leading_space"));
132 ret = get_uri_from_clfreq(" GET /space2 HTTP/1.1");
133 CX_TEST_ASSERT(!cx_strcmp(ret, "/space2"));
134 ret = get_uri_from_clfreq("GET /trailing_space HTTP/1.1");
135 CX_TEST_ASSERT(!cx_strcmp(ret, "/trailing_space"));
136 ret = get_uri_from_clfreq(" GET /space3 HTTP/1.1 ");
137 CX_TEST_ASSERT(!cx_strcmp(ret, "/space3"));
138
139 // fail test
140 ret = get_uri_from_clfreq("");
141 CX_TEST_ASSERT(ret.ptr == NULL);
142 ret = get_uri_from_clfreq(" ");
143 CX_TEST_ASSERT(ret.ptr == NULL);
144 ret = get_uri_from_clfreq("GET");
145 CX_TEST_ASSERT(ret.ptr == NULL);
146 ret = get_uri_from_clfreq("GET /path");
147 CX_TEST_ASSERT(ret.ptr == NULL);
148 ret = get_uri_from_clfreq(" /path2/ ");
149 CX_TEST_ASSERT(ret.ptr == NULL);
150 }
151 }
152
153 void http_reverse_proxy_add_tests(CxTestSuite *suite) {
154 cx_test_register(suite, test_safs_proxy_get_uri_from_clfreq);
155 }

mercurial