Klaus Demo bjoern / dfe42d3
Fix #139 Jonas Haag 1 year, 10 months ago
1 changed file(s) with 20 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
5656 *
5757 * Look into the returned iterator in any case. This allows us to do other
5858 * optimizations, for example if the returned value is a list with exactly
59 * one string in it, we can pick the string and throw away the list so bjoern
60 * does not have to come back again and look into the iterator a second time.
59 * one bytestring in it, we can pick the bytestring and throw away the list
60 * so bjoern does not have to come back again and look into the iterator a
61 * second time.
6162 */
6263 PyObject* first_chunk;
6364
6465 if(PyList_Check(retval) && PyList_GET_SIZE(retval) == 1 &&
6566 _PEP3333_Bytes_Check(PyList_GET_ITEM(retval, 0)))
6667 {
67 /* Optimize the most common case, a single string in a list: */
68 /* Optimize the most common case, a single bytestring in a list: */
6869 PyObject* tmp = PyList_GET_ITEM(retval, 0);
6970 Py_INCREF(tmp);
7071 Py_DECREF(retval);
7172 retval = tmp;
72 goto string; /* eeevil */
73 goto bytestring; /* eeevil */
7374 } else if(_PEP3333_Bytes_Check(retval)) {
7475 /* According to PEP 333 strings should be handled like any other iterable,
7576 * i.e. sending the response item for item. "item for item" means
76 * "char for char" if you have a string. -- I'm not that stupid. */
77 string:
77 * "char for char" if you have a bytestring. -- I'm not that stupid. */
78 bytestring:
79 request->iterable = NULL;
80 request->iterator = NULL;
7881 if(_PEP3333_Bytes_GET_SIZE(retval)) {
7982 first_chunk = retval;
8083 } else {
84 // empty response
8185 Py_DECREF(retval);
8286 first_chunk = NULL;
8387 }
107111 );
108112 Py_XDECREF(first_chunk);
109113 return false;
114 }
115
116 /* Special-case HTTP 204 and 304 */
117 if (!strncmp(_PEP3333_Bytes_AS_DATA(request->status), "204", 3) ||
118 !strncmp(_PEP3333_Bytes_AS_DATA(request->status), "304", 3)) {
119 request->state.response_length_unknown = false;
110120 }
111121
112122 /* keep-alive cruft */
347357 request->status = _PEP3333_BytesLatin1_FromUnicode(status_unicode);
348358 if (request->status == NULL) {
349359 return NULL;
360 } else if (_PEP3333_Bytes_GET_SIZE(request->status) < 3) {
361 PyErr_SetString(PyExc_ValueError, "'status' must be 3-digit");
362 Py_CLEAR(request->status);
363 return NULL;
350364 }
351365
352366 if(!inspect_headers(request)) {
354368 return NULL;
355369 }
356370
357 Py_INCREF(request->status);
358371 Py_INCREF(request->headers);
359372
360373 request->state.start_response_called = true;