resthttp-headersgsoap

gSoap - parse custom http header from request


I'm trying to find some information on how to parse out custom http headers received in a request by a gSoap server application. I've spent several hours trying to find any documentation on this but so far I haven't been successful.

I see plenty of documentation on how to set custom http headers for both the client and server (such as via the http_extra_header property), but not on how to read them from a request that was received. It seems like gSoap supports parsing out existing standardized headers (eg: X-Forwarded-For) but so far I can't figure out how to access headers that aren't already defined. I'm usually good at searching for stuff like this, but I keep hitting on documentation for gSoap header files, soap headers, or setting http headers. Nothing so far on receiving and parsing out custom headers that aren't already well-defined.

Any help is greatly appreciated.


Solution

  • You will need a callback function to process HTTP headers, see callback functions and in particular the fparsehdr callback:

    Callback that consumes an HTTP header that consists of a key-value pair.

    This callback is called by soap::fparse, consumes an HTTP header that is split in a key-value pair and updates the soap context state accordingly.

    Assign this callback and use the HTTP header key and value pair passed to it. Make sure to call the original soap->fparsehdr(soap, key, val) in the new callback, to let the engine process all headers by passing them through to the original callback:

    soap->user = (void*)soap->fparsehdr; // to call fparsehdr() in our callback
    soap->fparsehdr = my_parsehdr;
    

    The new callback function:

    typedef int(*PARSEFUNC)(struct soap*, const char*, const char*);
    int my_parsehdr(struct soap *soap, const char *key, const char *val)
    {
      ... // check key (non-NULL) and use val (non-NULL)
      return ((PARSEFUNC)(soap->user))(soap, key, val);
    }
    

    I recommend to pass all headers through to the engine, unless it is a custom header that has no meaning to the engine.