Klaus Demo bjoern / 3b46a6b
Fix of #122 (#123) * Fix of #122; Empty string if variable not defined. * add a simple wsgi validation test * remove linebreaks mentioned * init SERVER_[HOST|PORT] only once Simon Biewald authored 2 years ago Jonas Haag committed 2 years ago
4 changed file(s) with 67 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
3131 }
3232 }
3333
34 _initialize_request_module();
34 _initialize_request_module(&info);
3535 server_run(&info);
3636
3737 Py_RETURN_NONE;
201201 /* SERVER_PROTOCOL (REQUEST_PROTOCOL) */
202202 _set_header(_SERVER_PROTOCOL, parser->http_minor == 1 ? _HTTP_1_1 : _HTTP_1_0);
203203
204 /* SERVER_NAME and SERVER_PORT */
205 if (REQUEST->server_info->host) {
206 _set_header(_SERVER_NAME, REQUEST->server_info->host);
207 _set_header(_SERVER_PORT, REQUEST->server_info->port);
208 }
209
210204 /* REQUEST_METHOD */
211205 if(parser->method == HTTP_GET) {
212206 /* I love useless micro-optimizations. */
284278 on_header_value, on_headers_complete, on_body, on_message_complete
285279 };
286280
287 void _initialize_request_module()
281 void _initialize_request_module(ServerInfo* server_info)
288282 {
289283 IO_module = PyImport_ImportModule("io");
290284 if (IO_module == NULL) {
356350 "wsgi.run_once",
357351 Py_False
358352 );
359 }
360 }
353
354 /* dct['SERVER_NAME'] = '...'
355 * dct['SERVER_PORT'] = '...'
356 * Both are required by WSGI specs. */
357 if (server_info->host) {
358 PyDict_SetItemString(wsgi_base_dict, "SERVER_NAME", server_info->host);
359
360 if (server_info->port == Py_None) {
361 PyDict_SetItemString(wsgi_base_dict, "SERVER_PORT", PyUnicode_FromFormat(""));
362 } else {
363 PyDict_SetItemString(wsgi_base_dict, "SERVER_PORT", PyUnicode_FromFormat("%i", server_info->port));
364 }
365 } else {
366 /* SERVER_NAME is required, but not usefull with UNIX type sockets */
367 PyDict_SetItemString(wsgi_base_dict, "SERVER_NAME", PyUnicode_FromFormat(""));
368 PyDict_SetItemString(wsgi_base_dict, "SERVER_PORT", PyUnicode_FromFormat(""));
369 }
370 }
371 }
55 #include "common.h"
66 #include "server.h"
77
8 void _initialize_request_module(void);
8 void _initialize_request_module(ServerInfo* server_info);
99
1010 typedef struct {
1111 unsigned error_code : 2;
0 #!/usr/bin/env python
1
2 import os
3 import time
4
5 from multiprocessing import Process
6 from wsgiref.validate import validator
7
8 try:
9 from http import client as httplib
10 except ImportError: # Py 2
11 import httplib
12
13 import bjoern
14
15 @validator
16 def _app(environ, start_response):
17 start_response("200 OK", [("Content-Type", "text/plain")])
18 return [b"Hello World"]
19
20 def _start_server():
21 bjoern.run(_app, 'localhost', 8080)
22
23 def test_compliance():
24 p = Process(target=_start_server)
25 p.start()
26
27 time.sleep(3) # Should be enough for the server to start
28 try:
29 h = httplib.HTTPConnection('localhost', 8080)
30 h.request("GET", "/")
31 response = h.getresponse()
32 finally:
33 p.terminate()
34
35 assert response.reason == "OK"
36
37
38 if __name__ == "__main__":
39 try:
40 test_compliance()
41 except AssertionError:
42 print("Test failed")
43 else:
44 print("Test successful")