| 26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
| 27 */ |
27 */ |
| 28 |
28 |
| 29 #include "proxy.h" |
29 #include "proxy.h" |
| 30 |
30 |
| |
31 #include <sys/socket.h> |
| |
32 #include <arpa/inet.h> |
| |
33 |
| 31 #include "../util/pblock.h" |
34 #include "../util/pblock.h" |
| 32 |
|
| 33 #include "../proxy/httpclient.h" |
35 #include "../proxy/httpclient.h" |
| 34 |
36 |
| |
37 // Gets the uri part from an http request line |
| 35 static cxstring get_uri_from_clfreq(const char *clfreq) { |
38 static cxstring get_uri_from_clfreq(const char *clfreq) { |
| 36 cxstring uri = { NULL, 0 }; |
39 cxstring uri = { NULL, 0 }; |
| 37 |
40 |
| 38 const char *begin = NULL; |
41 const char *begin = NULL; |
| 39 const char *str = clfreq; |
42 const char *str = clfreq; |
| 69 |
72 |
| 70 return uri; |
73 return uri; |
| 71 |
74 |
| 72 } |
75 } |
| 73 |
76 |
| |
77 typedef struct ProxyData { |
| |
78 Session *sn; |
| |
79 Request *rq; |
| |
80 } ProxyData; |
| |
81 |
| |
82 static int proxy_response_start(HttpClient *client, int status, char *message, void *userdata) { |
| |
83 ProxyData *proxy = userdata; |
| |
84 |
| |
85 protocol_status(proxy->sn, proxy->rq, status, message); |
| |
86 protocol_start_response(proxy->sn, proxy->rq); |
| |
87 |
| |
88 return 0; |
| |
89 } |
| |
90 |
| |
91 static ssize_t proxy_response_write(HttpClient *client, void *buf, size_t nbytes, void *userdata) { |
| |
92 ProxyData *proxy = userdata; |
| |
93 ssize_t ret = net_write(proxy->sn->csd, buf, nbytes); |
| |
94 // TODO: handle errors |
| |
95 return ret; |
| |
96 } |
| |
97 |
| 74 int http_reverse_proxy_service(pblock *param, Session *sn, Request *rq) { |
98 int http_reverse_proxy_service(pblock *param, Session *sn, Request *rq) { |
| 75 EventHandler *ev = sn->ev; |
99 EventHandler *ev = sn->ev; |
| 76 const char *method = pblock_findkeyval(pb_key_method, rq->reqpb); |
100 const char *method = pblock_findkeyval(pb_key_method, rq->reqpb); |
| 77 const char *clfreq = pblock_findkeyval(pb_key_clf_request, rq->reqpb); |
101 const char *clfreq = pblock_findkeyval(pb_key_clf_request, rq->reqpb); |
| 78 |
102 |
| 95 if(http_client_set_uri_len(client, uri.ptr, uri.length)) { |
119 if(http_client_set_uri_len(client, uri.ptr, uri.length)) { |
| 96 http_client_free(client); |
120 http_client_free(client); |
| 97 return REQ_ABORTED; |
121 return REQ_ABORTED; |
| 98 } |
122 } |
| 99 |
123 |
| |
124 // test address |
| |
125 struct sockaddr_in address; |
| |
126 inet_pton(AF_INET, "127.0.0.1", &address.sin_addr); |
| |
127 address.sin_family = AF_INET; |
| |
128 address.sin_port = htons(8080); |
| |
129 http_client_set_addr(client, (struct sockaddr*)&address, sizeof(address)); |
| |
130 |
| 100 // add request headers to the client |
131 // add request headers to the client |
| 101 CxIterator i = pblock_iterator(rq->headers); |
132 CxIterator i = pblock_iterator(rq->headers); |
| 102 cx_foreach(pb_entry*, entry, i) { |
133 cx_foreach(pb_entry*, entry, i) { |
| 103 // TODO: don't pass all headers |
134 // TODO: don't pass all headers |
| 104 if(http_client_add_request_header(client, cx_mutstr(entry->param->name), cx_mutstr(entry->param->value))) { |
135 if(http_client_add_request_header(client, cx_mutstr(entry->param->name), cx_mutstr(entry->param->value))) { |
| 105 http_client_free(client); |
136 http_client_free(client); |
| 106 return REQ_ABORTED; |
137 return REQ_ABORTED; |
| 107 } |
138 } |
| 108 } |
139 } |
| 109 |
140 |
| |
141 ProxyData *proxy = pool_malloc(sn->pool, sizeof(ProxyData)); |
| |
142 proxy->sn = sn; |
| |
143 proxy->rq = rq; |
| |
144 client->response_start = proxy_response_start; |
| |
145 client->response_start_userdata = proxy; |
| |
146 client->response_body_write = proxy_response_write; |
| |
147 client->response_body_write_userdata = proxy; |
| |
148 |
| |
149 //net_setnonblock(sn->csd, 1); |
| 110 if(http_client_start(client)) { |
150 if(http_client_start(client)) { |
| |
151 //net_setnonblock(sn->csd, 0); |
| 111 http_client_free(client); |
152 http_client_free(client); |
| 112 return REQ_ABORTED; |
153 return REQ_ABORTED; |
| 113 } |
154 } |
| 114 |
155 |
| 115 return REQ_PROCESSING; |
156 return REQ_PROCESSING; |