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