crossroads

Git mirror of https://crossroads.e-tunity.com/
git clone git://git.finwo.net/app/crossroads
Log | Files | Refs

httpsetstickycookie.c (1244B)


      1 #include "crossroads.h"
      2 
      3 void http_set_sticky_cookie (unsigned char **bufp, int *buflen) {
      4     char
      5 	*cookieval = activeservice->backend[current_backend].insertcookie,
      6 	*instruction;
      7     unsigned char const *buf, *cp;
      8     unsigned char *newbuf;
      9     int crseen = 0, atend = 0;       
     10 
     11     cp = buf = *bufp;
     12     msg ("Pasting cookie '%s' in '%s'", cookieval, buf);
     13     while (1) {
     14 	if (*cp == '\n')
     15 	    cp++;
     16 	if (! (cp = (unsigned char const *)
     17 	        strchr ( (char const *) cp, '\n')) ) {
     18 	    msg ("Failed to find end of headers while inserting cookie");
     19 	    return;
     20 	}
     21 	cp++;
     22 	
     23 	if (*cp == '\r') {
     24 	    crseen++;
     25 	    if (*(cp + 1) == '\n')
     26 		atend++;
     27 	} else if (*cp == '\n')
     28 	    atend++;
     29 
     30 	if (atend) {
     31 	    instruction = str_printf ("Set-Cookie: %s%s",
     32 				      cookieval,
     33 				      crseen ? "\r\n" : "\n");
     34 	    
     35 	    newbuf = xmalloc (*buflen + strlen(instruction) + 1);
     36 	    
     37 	    memcpy (newbuf, buf, cp - buf);
     38 	    memcpy (newbuf + (cp - buf), instruction, strlen(instruction));
     39 	    memcpy (newbuf + (cp - buf) + strlen(instruction),
     40 		    cp, *buflen - (cp - buf));
     41 	    
     42 	    msg ("Modified buffer: '%s'", newbuf);
     43 
     44 	    free (*bufp);
     45 	    *bufp = newbuf;
     46 	    *buflen += strlen(instruction);
     47 	    
     48 	    return;
     49 	}
     50     }
     51 }