[FASTCGI] Cookie processing in Fast CGI

Tom Bowden charles_thomas at mac.com
Fri Dec 11 12:41:27 EST 2009


I decided not to go with JSON - since I can use maps/multimaps.  I am  
sticking with pcrecpp, but I will keep that logic in singleton  
patterns (the parsing).

What I am doing now is that I created an object (FCGXProcessor).  It  
has a series of operator<< functions, and maintains a deque of  
request objects that have a common base class (TRequestObject).  This  
deque holds the cookies, contents, and other variables as  TCookie,  
THeader, TContent, and TAnnotate (processor specific comments/ 
documentation).

The deque of objects is created from the environment variables, the  
stdin (FCGX_Request.in), and additional sources.

Once in the common format, I can manipulate as needed and then output  
the results of processor (which is a document to return to client).

FCGXProcessor p;
FCGI_Response r;

while ( 1 ) {
      if (  FCGX_Accept_r(&r) == 0 ) {
        p << r
     }
     .... MODIFY ...
     ostringstream out;
     out << p;
     FCGX_PutStr( out.c_str(), out.str().size(), r.out);

|





On Dec 10, 2009, at 2:33 PM, Martin Chapman wrote:

> My opinion would be parse it yourself so it's not dependent.  That  
> said,
> it's your project so do what makes the most sense for you.
>
> Martin
>
>
> -----Original Message-----
> From: Tom Bowden [mailto:charles_thomas at mac.com]
> Sent: Thursday, December 10, 2009 1:31 PM
> To: Martin Chapman
> Cc: 'FastCGI Developers'
> Subject: Re: [FASTCGI] Cookie processing in Fast CGI
>
> martin,
>
> A couple questions (compiled from previous post with an addition):
>
> I tend to use external libraries that make my life easier.  Namely
> jsoncpp and pcrecpp.
>
> With pcrecpp i can (for example) replace the parsing the logic where
> you're going character by character with something like:
>
> #define PCRE_COOKIE_MASK "^Set-Cookie:[ \t]*([^=]+)=(.*)$"
>
> and then:
> std::string cookie_name;
> std::string cookie_value;
> if ( pcre_object->PartialMatch(instr, &cookie_name, &cookie_value) ) {
>    // do assignment logic...
> }
>
> Obviously some times its not good to rely on third party libs
> (portability/etc).   Is this one of those times?  What would be most
> useful to the community?
>
> Tom
>
>
> On Dec 9, 2009, at 3:02 PM, Martin Chapman wrote:
>
>> Tom,
>>
>> It's a little ugly because it's COM and if you convert it to
>> FastCGI it
>> would be cool if you posted it back for the rest of us if you can.
>> The
>> entire COM implementation is attached.
>>
>> General Gist:
>>
>> 1. Get the cookie from the env vars.  Should be called HTTP_COOKIE.
>>
>> 2. Use a method like the one below to parse the cookie value.
>>
>> STDMETHODIMP CCookie::ParseValue(BSTR sCookie)
>> {
>> 	AFX_MANAGE_STATE(AfxGetStaticModuleState());
>>
>> 	_bstr_t bstrCookie(sCookie);
>> 	if (bstrCookie.length() == 0)
>> 		return E_INVALIDARG;
>>
>> 	CString strCookie = (TCHAR*) bstrCookie;
>> 	LPCSTR pEnd = strCookie;
>> 	LPCSTR pStart = strCookie;
>> 	CString name, value;
>>
>> 	while (*pEnd != '\0')
>> 	{
>> 		while (*pEnd && *pEnd != '=' && *pEnd != '&')
>> 			pEnd++;
>>
>> 		if (*pEnd == '\0' || *pEnd == '&')
>> 		{
>> 			if (pEnd > pStart)
>> 			{
>> 				CopyToCString(value, pStart, pEnd);
>> 			}
>> 			put_Value(_bstr_t(value.GetBuffer()));
>> 			if (*pEnd == '&')
>> 			{
>> 				pEnd++;
>> 				pStart = pEnd;
>> 				continue;
>> 			}
>> 			return S_OK;
>> 		}
>> 		else if (*pEnd == '=' )
>> 		{
>> 			if (pEnd > pStart)
>> 			{
>> 				CopyToCString(name, pStart, pEnd);
>> 			}
>> 			else
>> 			{
>> 				pEnd++;
>> 				pStart = pEnd;
>> 				break;
>> 			}
>>
>> 			pEnd++;
>> 			pStart = pEnd;
>> 			while (*pEnd && *pEnd != '&' && *pEnd != '=')
>> 				pEnd++;
>> 			if (pEnd > pStart)
>> 				CopyToCString(value, pStart, pEnd);
>>
>> 			AddValue(_bstr_t(name.GetBuffer()),
>> _bstr_t(value.GetBuffer()));
>> 			if (*pEnd != '\0')
>> 				pEnd++;
>> 			
>> 			pStart = pEnd;
>> 		}
>> 	}
>> 	
>> 	return S_OK;
>> }
>>
>> 3. The following func parses a single cookie value and is used in
>> the func
>> above.
>>
>> STDMETHODIMP CCookie::ParseValue(BSTR sCookie)
>> {
>> 	AFX_MANAGE_STATE(AfxGetStaticModuleState());
>>
>> 	_bstr_t bstrCookie(sCookie);
>> 	if (bstrCookie.length() == 0)
>> 		return E_INVALIDARG;
>>
>> 	CString strCookie = (TCHAR*) bstrCookie;
>> 	LPCSTR pEnd = strCookie;
>> 	LPCSTR pStart = strCookie;
>> 	CString name, value;
>>
>> 	while (*pEnd != '\0')
>> 	{
>> 		while (*pEnd && *pEnd != '=' && *pEnd != '&')
>> 			pEnd++;
>>
>> 		if (*pEnd == '\0' || *pEnd == '&')
>> 		{
>> 			if (pEnd > pStart)
>> 			{
>> 				CopyToCString(value, pStart, pEnd);
>> 			}
>> 			put_Value(_bstr_t(value.GetBuffer()));
>> 			if (*pEnd == '&')
>> 			{
>> 				pEnd++;
>> 				pStart = pEnd;
>> 				continue;
>> 			}
>> 			return S_OK;
>> 		}
>> 		else
>> 		if (*pEnd == '=' )
>> 		{
>> 			if (pEnd > pStart)
>> 			{
>> 				CopyToCString(name, pStart, pEnd);
>> 			}
>> 			else
>> 			{
>> 				pEnd++;
>> 				pStart = pEnd;
>> 				break;
>> 			}
>>
>> 			pEnd++;
>> 			pStart = pEnd;
>> 			while (*pEnd && *pEnd != '&' && *pEnd != '=')
>> 				pEnd++;
>> 			if (pEnd > pStart)
>> 				CopyToCString(value, pStart, pEnd);
>>
>> 			AddValue(_bstr_t(name.GetBuffer()),
>> _bstr_t(value.GetBuffer()));
>> 			if (*pEnd != '\0')
>> 				pEnd++;
>> 			
>> 			pStart = pEnd;
>> 		}
>> 	}
>> 	
>> 	return S_OK;
>> }
>>
>>
>> -----Original Message-----
>> From: fastcgi-developers-bounces
>> +chapmanm=pixia.com at mailman.fastcgi.com
>> [mailto:fastcgi-developers-bounces
>> +chapmanm=pixia.com at mailman.fastcgi.com]
>> On Behalf Of Tom Bowden
>> Sent: Wednesday, December 09, 2009 1:18 PM
>> To: FastCGI Developers
>> Subject: [FASTCGI] Cookie processing in Fast CGI
>>
>> Is there a thread somewhere that talks about parsing cookies - or, is
>> this something that I have to treat like any other header parsing
>> task.
>>
>> Tom
>>
>> _______________________________________________
>> FastCGI-developers mailing list
>> FastCGI-developers at mailman.fastcgi.com
>> http://mailman.pins.net/mailman/listinfo.cgi/fastcgi-
>> developers<CookieCollection.h><Cookie.cpp><Cookie.h><CookieCollection 
>> .
>> cpp>
>



More information about the FastCGI-developers mailing list