* one more place to NULL it
[citadel.git] / webcit / tcp_sockets.c
1 /*
2  * $Id$
3  */
4
5 /*
6  * Uncomment this to log all communications with the Citadel server
7 #define SERV_TRACE 1
8  */
9
10
11 #include "webcit.h"
12 #include "webserver.h"
13
14 extern int DisableGzip;
15
16 /*
17  * register the timeout
18  */
19 RETSIGTYPE timeout(int signum)
20 {
21         lprintf(1, "Connection timed out; unable to reach citserver\n");
22         /* no exit here, since we need to server the connection unreachable thing. exit(3); */
23 }
24
25
26 /*
27  *  Connect a unix domain socket
28  *  sockpath where to open a unix domain socket
29  */
30 int uds_connectsock(char *sockpath)
31 {
32         struct sockaddr_un addr;
33         int s;
34
35         memset(&addr, 0, sizeof(addr));
36         addr.sun_family = AF_UNIX;
37         strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
38
39         s = socket(AF_UNIX, SOCK_STREAM, 0);
40         if (s < 0) {
41                 lprintf(1, "Can't create socket[%s]: %s\n",
42                         sockpath,
43                         strerror(errno));
44                 return(-1);
45         }
46
47         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
48                 lprintf(1, "Can't connect [%s]: %s\n",
49                         sockpath,
50                         strerror(errno));
51                 close(s);
52                 return(-1);
53         }
54
55         return s;
56 }
57
58
59 /*
60  *  Connect a TCP/IP socket
61  *  host the host to connect to
62  *  service the service on the host to call
63  */
64 int tcp_connectsock(char *host, char *service)
65 {
66         int fdflags;
67         struct hostent *phe;
68         struct servent *pse;
69         struct protoent *ppe;
70         struct sockaddr_in sin;
71         int s;
72
73         memset(&sin, 0, sizeof(sin));
74         sin.sin_family = AF_INET;
75
76         pse = getservbyname(service, "tcp");
77         if (pse) {
78                 sin.sin_port = pse->s_port;
79         } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
80                 lprintf(1, "Can't get %s service entry\n", service);
81                 return (-1);
82         }
83         phe = gethostbyname(host);
84         if (phe) {
85                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
86         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
87                 lprintf(1, "Can't get %s host entry: %s\n",
88                         host, strerror(errno));
89                 return (-1);
90         }
91         if ((ppe = getprotobyname("tcp")) == 0) {
92                 lprintf(1, "Can't get TCP protocol entry: %s\n",
93                         strerror(errno));
94                 return (-1);
95         }
96
97         s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
98         if (s < 0) {
99                 lprintf(1, "Can't create socket: %s\n", strerror(errno));
100                 return (-1);
101         }
102
103         fdflags = fcntl(s, F_GETFL);
104         if (fdflags < 0)
105                 lprintf(1, "unable to get socket flags!  %s.%s: %s \n",
106                         host, service, strerror(errno));
107         fdflags = fdflags | O_NONBLOCK;
108         if (fcntl(s, F_SETFD, fdflags) < 0)
109                 lprintf(1, "unable to set socket nonblocking flags!  %s.%s: %s \n",
110                         host, service, strerror(errno));
111
112         signal(SIGALRM, timeout);
113         alarm(30);
114
115         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
116                 lprintf(1, "Can't connect to %s.%s: %s\n",
117                         host, service, strerror(errno));
118                 close(s);
119                 return (-1);
120         }
121         alarm(0);
122         signal(SIGALRM, SIG_IGN);
123         if (!is_https) {
124                 fdflags = fcntl(s, F_GETFL);
125                 if (fdflags < 0)
126                         lprintf(1, "unable to get socket flags!  %s.%s: %s \n",
127                                 host, service, strerror(errno));
128                 fdflags = fdflags | O_NONBLOCK;
129                 if (fcntl(s, F_SETFD, fdflags) < 0)
130                         lprintf(1, "unable to set socket nonblocking flags!  %s.%s: %s \n",
131                                 host, service, strerror(errno));
132         }
133         return (s);
134 }
135
136
137
138 /*
139  *  input string from pipe
140  */
141 int serv_getln(char *strbuf, int bufsize)
142 {
143         wcsession *WCC = WC;
144         int len;
145
146         *strbuf = '\0';
147         StrBuf_ServGetln(WCC->MigrateReadLineBuf);
148         len = StrLength(WCC->MigrateReadLineBuf);
149         if (len > bufsize)
150                 len = bufsize - 1;
151         memcpy(strbuf, ChrPtr(WCC->MigrateReadLineBuf), len);
152         FlushStrBuf(WCC->MigrateReadLineBuf);
153         strbuf[len] = '\0';
154 #ifdef SERV_TRACE
155         lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
156 #endif
157         return len;
158 }
159
160
161 int StrBuf_ServGetln(StrBuf *buf)
162 {
163         wcsession *WCC = WC;
164         const char *ErrStr = NULL;
165         int rc;
166
167         rc = StrBufTCP_read_buffered_line_fast(buf, 
168                                                WCC->ReadBuf, 
169                                                &WCC->ReadPos, 
170                                                &WCC->serv_sock, 
171                                                5, 1, 
172                                                &ErrStr);
173         if (rc < 0)
174         {
175                 lprintf(1, "Server connection broken: %s\n",
176                         (ErrStr)?ErrStr:"");
177                 wc_backtrace();
178                 WCC->serv_sock = (-1);
179                 WCC->connected = 0;
180                 WCC->logged_in = 0;
181         }
182         return rc;
183 }
184
185 int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
186 {
187         wcsession *WCC = WC;
188         const char *ErrStr;
189         int rc;
190         
191         rc = StrBufReadBLOBBuffered(buf, 
192                                     WCC->ReadBuf, 
193                                     &WCC->ReadPos,
194                                     &WCC->serv_sock, 
195                                     1, 
196                                     BlobSize, 
197                                     NNN_TERM,
198                                     &ErrStr);
199         if (rc < 0)
200         {
201                 lprintf(1, "Server connection broken: %s\n",
202                         (ErrStr)?ErrStr:"");
203                 wc_backtrace();
204                 WCC->serv_sock = (-1);
205                 WCC->connected = 0;
206                 WCC->logged_in = 0;
207         }
208         return rc;
209 }
210
211 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
212 {
213         wcsession *WCC = WC;
214         const char *ErrStr;
215         int rc;
216         
217         WCC->ReadPos = NULL;
218         rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &ErrStr);
219         if (rc < 0)
220         {
221                 lprintf(1, "Server connection broken: %s\n",
222                         (ErrStr)?ErrStr:"");
223                 wc_backtrace();
224                 WCC->serv_sock = (-1);
225                 WCC->connected = 0;
226                 WCC->logged_in = 0;
227         }
228         return rc;
229 }
230
231 /*
232  *  send binary to server
233  *  buf the buffer to write to citadel server
234  *  nbytes how many bytes to send to citadel server
235  */
236 void serv_write(const char *buf, int nbytes)
237 {
238         wcsession *WCC = WC;
239         int bytes_written = 0;
240         int retval;
241
242         FlushStrBuf(WCC->ReadBuf);
243         WCC->ReadPos = NULL;
244         while (bytes_written < nbytes) {
245                 retval = write(WCC->serv_sock, &buf[bytes_written],
246                                nbytes - bytes_written);
247                 if (retval < 1) {
248                         const char *ErrStr = strerror(errno);
249                         lprintf(1, "Server connection broken: %s\n",
250                                 (ErrStr)?ErrStr:"");
251                         close(WCC->serv_sock);
252                         WCC->serv_sock = (-1);
253                         WCC->connected = 0;
254                         WCC->logged_in = 0;
255                         return;
256                 }
257                 bytes_written = bytes_written + retval;
258         }
259 }
260
261
262 /*
263  *  send line to server
264  *  string the line to send to the citadel server
265  */
266 void serv_puts(const char *string)
267 {
268         wcsession *WCC = WC;
269 #ifdef SERV_TRACE
270         lprintf(9, "%3d<%s\n", WC->serv_sock, string);
271 #endif
272         FlushStrBuf(WCC->ReadBuf);
273         WCC->ReadPos = NULL;
274
275         serv_write(string, strlen(string));
276         serv_write("\n", 1);
277 }
278
279 /*
280  *  send line to server
281  *  string the line to send to the citadel server
282  */
283 void serv_putbuf(const StrBuf *string)
284 {
285         wcsession *WCC = WC;
286 #ifdef SERV_TRACE
287         lprintf(9, "%3d<%s\n", WC->serv_sock, ChrPtr(string));
288 #endif
289         FlushStrBuf(WCC->ReadBuf);
290         WCC->ReadPos = NULL;
291
292         serv_write(ChrPtr(string), StrLength(string));
293         serv_write("\n", 1);
294 }
295
296
297 /*
298  *  convenience function to send stuff to the server
299  *  format the formatstring
300  *  ... the entities to insert into format 
301  */
302 void serv_printf(const char *format,...)
303 {
304         wcsession *WCC = WC;
305         va_list arg_ptr;
306         char buf[SIZ];
307         size_t len;
308
309         FlushStrBuf(WCC->ReadBuf);
310         WCC->ReadPos = NULL;
311
312         va_start(arg_ptr, format);
313         vsnprintf(buf, sizeof buf, format, arg_ptr);
314         va_end(arg_ptr);
315
316         len = strlen(buf);
317         buf[len++] = '\n';
318         buf[len] = '\0';
319         serv_write(buf, len);
320 #ifdef SERV_TRACE
321         lprintf(9, "<%s", buf);
322 #endif
323 }
324
325
326
327
328 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
329 {
330         const char *Error, *pch, *pchs;
331         int rlen, len, retval = 0;
332
333 #ifdef HAVE_OPENSSL
334         if (is_https) {
335                 int ntries = 0;
336                 if (StrLength(Hdr->ReadBuf) > 0) {
337                         pchs = ChrPtr(Hdr->ReadBuf);
338                         pch = strchr(pchs, '\n');
339                         if (pch != NULL) {
340                                 rlen = 0;
341                                 len = pch - pchs;
342                                 if (len > 0 && (*(pch - 1) == '\r') )
343                                         rlen ++;
344                                 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
345                                 StrBufCutLeft(Hdr->ReadBuf, len + 1);
346                                 return len - rlen;
347                         }
348                 }
349
350                 while (retval == 0) { 
351                                 pch = NULL;
352                                 pchs = ChrPtr(Hdr->ReadBuf);
353                                 if (*pchs != '\0')
354                                         pch = strchr(pchs, '\n');
355                                 if (pch == NULL) {
356                                         retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
357                                         pchs = ChrPtr(Hdr->ReadBuf);
358                                         pch = strchr(pchs, '\n');
359                                 }
360                                 if (retval == 0) {
361                                         sleeeeeeeeeep(1);
362                                         ntries ++;
363                                 }
364                                 if (ntries > 10)
365                                         return 0;
366                 }
367                 if ((retval > 0) && (pch != NULL)) {
368                         rlen = 0;
369                         len = pch - pchs;
370                         if (len > 0 && (*(pch - 1) == '\r') )
371                                 rlen ++;
372                         StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
373                         StrBufCutLeft(Hdr->ReadBuf, len + 1);
374                         return len - rlen;
375
376                 }
377                 else 
378                         return -1;
379         }
380         else 
381 #endif
382                 return StrBufTCP_read_buffered_line_fast(Target, 
383                                                          Hdr->ReadBuf,
384                                                          &Hdr->Pos,
385                                                          &Hdr->http_sock,
386                                                          5,
387                                                          1,
388                                                          &Error);
389 }
390
391 /* 
392  * This is a generic function to set up a master socket for listening on
393  * a TCP port.  The server shuts down if the bind fails.
394  *
395  * ip_addr      IP address to bind
396  * port_number  port number to bind
397  * queue_len    number of incoming connections to allow in the queue
398  */
399 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
400 {
401         struct protoent *p;
402         struct sockaddr_in sin;
403         int s, i;
404
405         memset(&sin, 0, sizeof(sin));
406         sin.sin_family = AF_INET;
407         if (ip_addr == NULL) {
408                 sin.sin_addr.s_addr = INADDR_ANY;
409         } else {
410                 sin.sin_addr.s_addr = inet_addr(ip_addr);
411         }
412
413         if (sin.sin_addr.s_addr == INADDR_NONE) {
414                 sin.sin_addr.s_addr = INADDR_ANY;
415         }
416
417         if (port_number == 0) {
418                 lprintf(1, "Cannot start: no port number specified.\n");
419                 return (-WC_EXIT_BIND);
420         }
421         sin.sin_port = htons((u_short) port_number);
422
423         p = getprotobyname("tcp");
424
425         s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
426         if (s < 0) {
427                 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
428                 return (-WC_EXIT_BIND);
429         }
430         /* Set some socket options that make sense. */
431         i = 1;
432         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
433
434         #ifndef __APPLE__
435         fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
436                                           there should be a preceding F_GETFL
437                                           and a bitwise OR with the previous
438                                           fd flags */
439         #endif
440         
441         if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
442                 lprintf(1, "Can't bind: %s\n", strerror(errno));
443                 return (-WC_EXIT_BIND);
444         }
445         if (listen(s, queue_len) < 0) {
446                 lprintf(1, "Can't listen: %s\n", strerror(errno));
447                 return (-WC_EXIT_BIND);
448         }
449         return (s);
450 }
451
452
453
454 /*
455  * Create a Unix domain socket and listen on it
456  * sockpath - file name of the unix domain socket
457  * queue_len - Number of incoming connections to allow in the queue
458  */
459 int ig_uds_server(char *sockpath, int queue_len)
460 {
461         struct sockaddr_un addr;
462         int s;
463         int i;
464         int actual_queue_len;
465
466         actual_queue_len = queue_len;
467         if (actual_queue_len < 5) actual_queue_len = 5;
468
469         i = unlink(sockpath);
470         if ((i != 0) && (errno != ENOENT)) {
471                 lprintf(1, "webcit: can't unlink %s: %s\n",
472                         sockpath, strerror(errno));
473                 return (-WC_EXIT_BIND);
474         }
475
476         memset(&addr, 0, sizeof(addr));
477         addr.sun_family = AF_UNIX;
478         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
479
480         s = socket(AF_UNIX, SOCK_STREAM, 0);
481         if (s < 0) {
482                 lprintf(1, "webcit: Can't create a socket: %s\n",
483                         strerror(errno));
484                 return (-WC_EXIT_BIND);
485         }
486
487         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
488                 lprintf(1, "webcit: Can't bind: %s\n",
489                         strerror(errno));
490                 return (-WC_EXIT_BIND);
491         }
492
493         if (listen(s, actual_queue_len) < 0) {
494                 lprintf(1, "webcit: Can't listen: %s\n",
495                         strerror(errno));
496                 return (-WC_EXIT_BIND);
497         }
498
499         chmod(sockpath, 0777);
500         return(s);
501 }
502
503
504
505
506 /*
507  * Read data from the client socket.
508  *
509  * sock         socket fd to read from
510  * buf          buffer to read into 
511  * bytes        number of bytes to read
512  * timeout      Number of seconds to wait before timing out
513  *
514  * Possible return values:
515  *      1       Requested number of bytes has been read.
516  *      0       Request timed out.
517  *      -1      Connection is broken, or other error.
518  */
519 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
520 {
521         const char *Error;
522         int retval = 0;
523
524 #ifdef HAVE_OPENSSL
525         if (is_https) {
526                 long bufremain;
527                 long baselen;
528
529                 baselen = StrLength(Target);
530
531                 if (Hdr->Pos == NULL)
532                         Hdr->Pos = ChrPtr(Hdr->ReadBuf);
533                 bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
534
535                 if (bytes < bufremain)
536                         bufremain = bytes;
537                 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
538                 StrBufCutLeft(Hdr->ReadBuf, bufremain);
539
540                 if (bytes > bufremain) 
541                 {
542                         while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
543                                (retval >= 0))
544                                 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
545                         if (retval >= 0) {
546                                 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
547 #ifdef HTTP_TRACING
548                                 write(2, "\033[32m", 5);
549                                 write(2, buf, bytes);
550                                 write(2, "\033[30m", 5);
551 #endif
552                                 return 1;
553                         }
554                         else {
555                                 lprintf(2, "client_read_ssl() failed\n");
556                                 return -1;
557                         }
558                 }
559                 else 
560                         return 1;
561         }
562 #endif
563
564         retval = StrBufReadBLOBBuffered(Target, 
565                                         Hdr->ReadBuf, 
566                                         &Hdr->Pos, 
567                                         &Hdr->http_sock, 
568                                         1, 
569                                         bytes,
570                                         O_TERM,
571                                         &Error);
572         if (retval < 0) {
573                 lprintf(2, "client_read() failed: %s\n",
574                         Error);
575                 return retval;
576         }
577
578 #ifdef HTTP_TRACING
579         write(2, "\033[32m", 5);
580         write(2, buf, bytes);
581         write(2, "\033[30m", 5);
582 #endif
583         return 1;
584 }
585
586
587 /*
588  * Begin buffering HTTP output so we can transmit it all in one write operation later.
589  */
590 void begin_burst(void)
591 {
592         if (WC->WBuf == NULL) {
593                 WC->WBuf = NewStrBufPlain(NULL, 32768);
594         }
595 }
596
597
598 /*
599  * Finish buffering HTTP output.  [Compress using zlib and] output with a Content-Length: header.
600  */
601 long end_burst(void)
602 {
603         wcsession *WCC = WC;
604         const char *ptr, *eptr;
605         long count;
606         ssize_t res = 0;
607         fd_set wset;
608         int fdflags;
609
610         if (!DisableGzip && (WCC->Hdr->HR.gzip_ok))
611         {
612                 if (CompressBuffer(WCC->WBuf) > 0)
613                         hprintf("Content-encoding: gzip\r\n");
614                 else {
615                         lprintf(CTDL_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
616                         wc_backtrace();
617                 }
618         }
619
620         if (WCC->Hdr->HR.prohibit_caching)
621                 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
622         hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
623
624         ptr = ChrPtr(WCC->HBuf);
625         count = StrLength(WCC->HBuf);
626         eptr = ptr + count;
627
628 #ifdef HAVE_OPENSSL
629         if (is_https) {
630                 client_write_ssl(WCC->HBuf);
631                 client_write_ssl(WCC->WBuf);
632                 return (count);
633         }
634 #endif
635
636         
637 #ifdef HTTP_TRACING
638         
639         write(2, "\033[34m", 5);
640         write(2, ptr, StrLength(WCC->WBuf));
641         write(2, "\033[30m", 5);
642 #endif
643         if (WCC->Hdr->http_sock == -1)
644                 return -1;
645         fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
646
647         while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)){
648                 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
649                         FD_ZERO(&wset);
650                         FD_SET(WCC->Hdr->http_sock, &wset);
651                         if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
652                                 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
653                                 return -1;
654                         }
655                 }
656
657                 if ((WCC->Hdr->http_sock == -1) || 
658                     (res = write(WCC->Hdr->http_sock, 
659                                  ptr,
660                                  count)) == -1) {
661                         lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
662                         wc_backtrace();
663                         return res;
664                 }
665                 count -= res;
666                 ptr += res;
667         }
668
669         ptr = ChrPtr(WCC->WBuf);
670         count = StrLength(WCC->WBuf);
671         eptr = ptr + count;
672
673 #ifdef HTTP_TRACING
674         
675         write(2, "\033[34m", 5);
676         write(2, ptr, StrLength(WCC->WBuf));
677         write(2, "\033[30m", 5);
678 #endif
679
680         while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
681                 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
682                         FD_ZERO(&wset);
683                         FD_SET(WCC->Hdr->http_sock, &wset);
684                         if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
685                                 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
686                                 return -1;
687                         }
688                 }
689
690                 if ((WCC->Hdr->http_sock == -1) || 
691                     (res = write(WCC->Hdr->http_sock, 
692                                  ptr,
693                                  count)) == -1) {
694                         lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
695                         wc_backtrace();
696                         return res;
697                 }
698                 count -= res;
699                 ptr += res;
700         }
701
702         return StrLength(WCC->WBuf);
703 }
704
705
706 /*
707  * lingering_close() a`la Apache. see
708  * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
709  */
710 int lingering_close(int fd)
711 {
712         char buf[SIZ];
713         int i;
714         fd_set set;
715         struct timeval tv, start;
716
717         gettimeofday(&start, NULL);
718         if (fd == -1)
719                 return -1;
720         shutdown(fd, 1);
721         do {
722                 do {
723                         gettimeofday(&tv, NULL);
724                         tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
725                         tv.tv_usec = start.tv_usec - tv.tv_usec;
726                         if (tv.tv_usec < 0) {
727                                 tv.tv_sec--;
728                                 tv.tv_usec += 1000000;
729                         }
730                         FD_ZERO(&set);
731                         FD_SET(fd, &set);
732                         i = select(fd + 1, &set, NULL, NULL, &tv);
733                 } while (i == -1 && errno == EINTR);
734
735                 if (i <= 0)
736                         break;
737
738                 i = read(fd, buf, sizeof buf);
739         } while (i != 0 && (i != -1 || errno == EINTR));
740
741         return close(fd);
742 }
743
744 void
745 HttpNewModule_TCPSOCKETS
746 (ParsedHttpHdrs *httpreq)
747 {
748
749         httpreq->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
750 }
751
752 void
753 HttpDetachModule_TCPSOCKETS
754 (ParsedHttpHdrs *httpreq)
755 {
756
757         FlushStrBuf(httpreq->ReadBuf);
758         ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
759 }
760
761 void
762 HttpDestroyModule_TCPSOCKETS
763 (ParsedHttpHdrs *httpreq)
764 {
765
766         FreeStrBuf(&httpreq->ReadBuf);
767 }
768
769
770 void
771 SessionNewModule_TCPSOCKETS
772 (wcsession *sess)
773 {
774         sess->CLineBuf = NewStrBuf();
775         sess->MigrateReadLineBuf = NewStrBuf();
776 }
777
778 void 
779 SessionDestroyModule_TCPSOCKETS
780 (wcsession *sess)
781 {
782         FreeStrBuf(&sess->CLineBuf);
783         FreeStrBuf(&sess->ReadBuf);
784         sess->ReadPos = NULL;
785         FreeStrBuf(&sess->MigrateReadLineBuf);
786         if (sess->serv_sock > 0)
787                 close(sess->serv_sock);
788 }