Klaus Demo bjoern / f0fbe78
Add REMOTE_ADDR to the WSGI environ. Closes #18 Jonas Haag 9 years ago
3 changed file(s) with 18 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
1111 static PyObject* wsgi_base_dict;
1212
1313
14 Request* Request_new(int client_fd)
14 Request* Request_new(int client_fd, const char* client_addr)
1515 {
1616 Request* req = _Request_from_prealloc();
1717 if(req == NULL)
2727 #endif
2828
2929 req->client_fd = client_fd;
30 req->headers = PyDict_New();
31 PyDict_SetItem(req->headers, _REMOTE_ADDR, PyString_FromString(client_addr));
3032 http_parser_init((http_parser*)&req->parser, HTTP_REQUEST);
3133 req->parser.parser.data = req;
3234
140142 static int
141143 on_message_begin(http_parser* parser)
142144 {
143 REQUEST->headers = PyDict_New();
144145 PARSER->field_start = NULL;
145146 PARSER->field_len = 0;
146147 PARSER->value_start = NULL;
361362 memset(_preallocd_used, 0, sizeof(char)*REQUEST_PREALLOC_N);
362363
363364 #define _(name) _##name = PyString_FromString(#name)
365 _(REMOTE_ADDR);
364366 _(PATH_INFO);
365367 _(QUERY_STRING);
366368 _(REQUEST_URI);
3131 ev_io ev_watcher;
3232
3333 bj_parser parser;
34 PyObject* headers; /* rm. */
34 PyObject* headers;
3535 PyObject* body;
3636
3737 PyObject* current_chunk;
4040 PyObject* status;
4141 } Request;
4242
43 Request* Request_new(int client_fd);
43 Request* Request_new(int client_fd, const char* client_addr);
4444 void Request_parse(Request*, const char*, const size_t);
4545 void Request_free(Request*);
4646
4747
4848 static PyObject
49 * _REMOTE_ADDR,
4950 * _PATH_INFO,
5051 * _QUERY_STRING,
5152 * _REQUEST_URI,
7676 ev_io_on_request(struct ev_loop* mainloop, ev_io* watcher, const int events)
7777 {
7878 int client_fd;
79 if((client_fd = accept(watcher->fd, NULL, NULL)) < 0) {
79 struct sockaddr_in sockaddr;
80 socklen_t addrlen;
81
82 addrlen = sizeof(struct sockaddr_in);
83 if((client_fd = accept(watcher->fd, &sockaddr, &addrlen)) < 0) {
8084 DBG("Could not accept() client: errno %d", errno);
8185 return;
8286 }
8690 return;
8791 }
8892
89 Request* request = Request_new(client_fd);
93 GIL_LOCK(0);
94 Request* request = Request_new(client_fd, inet_ntoa(sockaddr.sin_addr));
95 GIL_UNLOCK(0);
96
97 DBG_REQ(request, "Accepted client %s:%d on fd %d",
98 inet_ntoa(sockaddr.sin_addr), ntohs(sockaddr.sin_port), client_fd);
9099
91100 ev_io_init(&request->ev_watcher, &ev_io_on_read, client_fd, EV_READ);
92101 ev_io_start(mainloop, &request->ev_watcher);