@pymethod string|HTTP_FILTER_CONTEXT|GetServerVariable| @rdesc The result is a string object, unless the server variable name begins with 'UNICODE_', in which case it is a unicode object - see the ISAPI docs for more details.
| 247 | // begins with 'UNICODE_', in which case it is a unicode object - see the |
| 248 | // ISAPI docs for more details. |
| 249 | PyObject * PyHFC::GetServerVariable(PyObject *self, PyObject *args) |
| 250 | { |
| 251 | BOOL bRes = FALSE; |
| 252 | char *variable = NULL; |
| 253 | PyObject *def = NULL; |
| 254 | |
| 255 | PyHFC * phfc = (PyHFC *) self; |
| 256 | |
| 257 | // @pyparm string|variable|| |
| 258 | // @pyparm object|default||If specified, the function will return this |
| 259 | // value instead of raising an error if the variable could not be fetched. |
| 260 | if (!PyArg_ParseTuple(args, "s|O:GetServerVariable", &variable, &def)) |
| 261 | return NULL; |
| 262 | |
| 263 | char buf[8192] = ""; |
| 264 | DWORD bufsize = sizeof(buf)/sizeof(buf[0]); |
| 265 | char *bufUse = buf; |
| 266 | if (phfc->m_pfc){ |
| 267 | bRes = phfc->m_pfc->GetServerVariable(variable, buf, &bufsize); |
| 268 | if (!bRes && GetLastError() == ERROR_INSUFFICIENT_BUFFER) { |
| 269 | // Although the IIS docs say it should be good, IIS5 |
| 270 | // returns -1 for 'bufsize' and MS samples show not |
| 271 | // to trust it too. Like the MS sample, we max out |
| 272 | // at some value - we choose 64k. We double each |
| 273 | // time, meaning we get 3 goes around the loop |
| 274 | bufUse = NULL; |
| 275 | bufsize = sizeof(buf); |
| 276 | for (int i=0;i<3;i++) { |
| 277 | bufsize *= 2; |
| 278 | bufUse = (char *)realloc(bufUse, bufsize); |
| 279 | if (!bufUse) |
| 280 | break; |
| 281 | bRes = phfc->m_pfc->GetServerVariable(variable, bufUse, &bufsize); |
| 282 | if (bRes || GetLastError() != ERROR_INSUFFICIENT_BUFFER) |
| 283 | break; |
| 284 | } |
| 285 | } |
| 286 | if (!bufUse) |
| 287 | return PyErr_NoMemory(); |
| 288 | if (!bRes) { |
| 289 | if (bufUse != buf) |
| 290 | free(bufUse); |
| 291 | if (def) { |
| 292 | Py_INCREF(def); |
| 293 | return def; |
| 294 | } |
| 295 | return SetPyHFCError("GetServerVariable"); |
| 296 | } |
| 297 | } |
| 298 | PyObject *ret = strncmp("UNICODE_", variable, 8) == 0 ? |
| 299 | PyUnicode_FromWideChar((WCHAR *)bufUse, bufsize / sizeof(WCHAR)) : |
| 300 | PyString_FromStringAndSize(bufUse, bufsize); |
| 301 | if (bufUse != buf) |
| 302 | free(bufUse); |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | // @pymethod |HTTP_FILTER_CONTEXT|SendResponseHeader| |