httpinsertheader.c (1530B)
1 /************************************************************************* 2 * This file is part of Crosroads 1.23, a load balancer and fail over 3 * utility for TCP. Copyright (c) Karel Kubat, distributed under GPL. 4 * Visit http://crossroads.e-tunity.com for information. 5 *************************************************************************/ 6 #include "crossroads.h" 7 8 void http_insert_header (unsigned char **bufp, unsigned *buflen, 9 char const *header) { 10 unsigned char const *buf, *cp; 11 unsigned char *newbuf; 12 char *instruction; 13 int crseen = 0, atend = 0; 14 15 cp = buf = *bufp; 16 msg ("Pasting header '%s' in '%s'", header, buf); 17 while (1) { 18 if (*cp == '\n') 19 cp++; 20 if (! (cp = (unsigned char const *) 21 strchr ( (char const *) cp, '\n')) ) { 22 msg ("Failed to find end of headers while inserting header"); 23 return; 24 } 25 cp++; 26 27 if (*cp == '\r') { 28 crseen++; 29 if (*(cp + 1) == '\n') 30 atend++; 31 } else if (*cp == '\n') 32 atend++; 33 34 if (atend) { 35 instruction = str_printf ("%s%s", 36 header, 37 crseen ? "\r\n" : "\n"); 38 39 newbuf = xmalloc (*buflen + strlen(instruction) + 1); 40 41 memcpy (newbuf, buf, cp - buf); 42 memcpy (newbuf + (cp - buf), instruction, strlen(instruction)); 43 memcpy (newbuf + (cp - buf) + strlen(instruction), 44 cp, *buflen - (cp - buf)); 45 46 msg ("Modified buffer: '%s'", newbuf); 47 48 free (*bufp); 49 *bufp = newbuf; 50 *buflen += strlen(instruction); 51 52 return; 53 } 54 } 55 }