MCPcopy Create free account
hub / github.com/mhammond/pywin32 / GetServerVariable

Method GetServerVariable

isapi/src/PyExtensionObjects.cpp:453–508  ·  view source on GitHub ↗

@pymethod string|EXTENSION_CONTROL_BLOCK|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.

Source from the content-addressed store, hash-verified

451// begins with 'UNICODE_', in which case it is a unicode object - see the
452// ISAPI docs for more details.
453PyObject * PyECB::GetServerVariable(PyObject *self, PyObject *args)
454{
455 BOOL bRes = FALSE;
456 char *variable = NULL;
457 PyObject *def = NULL;
458
459 PyECB * pecb = (PyECB *) self;
460 // @pyparm string|variable||
461 // @pyparm object|default||If specified, the function will return this
462 // value instead of raising an error if the variable could not be fetched.
463 if (!PyArg_ParseTuple(args, "s|O:GetServerVariable", &variable, &def))
464 return NULL;
465
466 char buf[8192] = "";
467 DWORD bufsize = sizeof(buf);
468 char *bufUse = buf;
469
470 if (pecb->m_pcb){
471 bRes = pecb->m_pcb->GetServerVariable(variable, buf, &bufsize);
472 if (!bRes && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
473 // Although the IIS docs say it should be good, IIS5
474 // returns -1 for 'bufsize' and MS samples show not
475 // to trust it too. Like the MS sample, we max out
476 // at some value - we choose 64k. We double each
477 // time, meaning we get 3 goes around the loop
478 bufUse = NULL;
479 bufsize = sizeof(buf);
480 for (int i=0;i<3;i++) {
481 bufsize *= 2;
482 bufUse = (char *)realloc(bufUse, bufsize);
483 if (!bufUse)
484 break;
485 bRes = pecb->m_pcb->GetServerVariable(variable, bufUse, &bufsize);
486 if (bRes || GetLastError() != ERROR_INSUFFICIENT_BUFFER)
487 break;
488 }
489 }
490 if (!bufUse)
491 return PyErr_NoMemory();
492 if (!bRes) {
493 if (bufUse != buf)
494 free(bufUse);
495 if (def) {
496 Py_INCREF(def);
497 return def;
498 }
499 return SetPyECBError("GetServerVariable");
500 }
501 }
502 PyObject *ret = strncmp("UNICODE_", variable, 8) == 0 ?
503 PyUnicode_FromWideChar((WCHAR *)bufUse, bufsize / sizeof(WCHAR)) :
504 PyString_FromStringAndSize(bufUse, bufsize);
505 if (bufUse != buf)
506 free(bufUse);
507 return ret;
508}
509
510// @pymethod string|EXTENSION_CONTROL_BLOCK|ReadClient|

Callers

nothing calls this directly

Calls 1

SetPyECBErrorFunction · 0.85

Tested by

no test coverage detected