Mercurial > urweb
comparison src/c/fastcgi.c @ 1762:a6eab6820b37
Lance Hepler's fix to configure.ac; small refactor of fastcgi.c to avoid undefined behavior
author | Adam Chlipala <adam@chlipala.net> |
---|---|
date | Sun, 13 May 2012 17:41:21 -0400 |
parents | ea131de361d9 |
children | 3d922a28370b |
comparison
equal
deleted
inserted
replaced
1761:f8ddaa296115 | 1762:a6eab6820b37 |
---|---|
38 FCGI_Record r; | 38 FCGI_Record r; |
39 int sock; | 39 int sock; |
40 } FCGI_Output; | 40 } FCGI_Output; |
41 | 41 |
42 typedef struct { | 42 typedef struct { |
43 char buf[sizeof(FCGI_Record) + 255]; | 43 FCGI_Record r; |
44 int available, used, sock; | 44 int available, used, sock; |
45 } FCGI_Input; | 45 } FCGI_Input; |
46 | 46 |
47 static FCGI_Output *fastcgi_output() { | 47 static FCGI_Output *fastcgi_output() { |
48 FCGI_Output *o = malloc(sizeof(FCGI_Output)); | 48 FCGI_Output *o = malloc(sizeof(FCGI_Output)); |
74 o->r.contentLengthB1 = contentLength >> 8; | 74 o->r.contentLengthB1 = contentLength >> 8; |
75 o->r.contentLengthB0 = contentLength & 255; | 75 o->r.contentLengthB0 = contentLength & 255; |
76 return uw_really_send(o->sock, &o->r, sizeof(o->r) - 65535 + contentLength); | 76 return uw_really_send(o->sock, &o->r, sizeof(o->r) - 65535 + contentLength); |
77 } | 77 } |
78 | 78 |
79 #define LATEST(i) ((FCGI_Record *)(i->buf + i->used)) | |
80 | |
81 static FCGI_Record *fastcgi_recv(FCGI_Input *i) { | 79 static FCGI_Record *fastcgi_recv(FCGI_Input *i) { |
80 if (i->used > 0) { | |
81 memmove((void*)&i->r, (void*)&i->r + i->used, i->available - i->used); | |
82 i->available -= i->used; | |
83 i->used = 0; | |
84 } | |
85 | |
82 while (1) { | 86 while (1) { |
83 ssize_t n; | 87 ssize_t n; |
84 | 88 |
85 if (i->available >= i->used + sizeof(FCGI_Record) - 65535 | 89 if (i->available >= sizeof(FCGI_Record) - 65535 |
86 && i->available >= i->used + sizeof(FCGI_Record) - 65535 | 90 && i->available >= sizeof(FCGI_Record) - 65535 |
87 + ((LATEST(i)->contentLengthB1 << 8) | LATEST(i)->contentLengthB0) | 91 + ((i->r.contentLengthB1 << 8) | i->r.contentLengthB0) |
88 + LATEST(i)->paddingLength) { | 92 + i->r.paddingLength) { |
89 FCGI_Record *r = LATEST(i); | 93 i->used = sizeof(FCGI_Record) - 65535 |
90 | 94 + ((i->r.contentLengthB1 << 8) | i->r.contentLengthB0) |
91 i->used += sizeof(FCGI_Record) - 65535 | 95 + i->r.paddingLength; |
92 + ((LATEST(i)->contentLengthB1 << 8) | LATEST(i)->contentLengthB0) | |
93 + LATEST(i)->paddingLength; | |
94 | 96 |
95 return r; | 97 return &i->r; |
96 } | 98 } |
97 | 99 |
98 if (i->used > 0) { | 100 n = recv(i->sock, (void*)&i->r + i->available, sizeof(i->r) - i->available, 0); |
99 memmove(i->buf, i->buf + i->used, i->available - i->used); | |
100 i->available -= i->used; | |
101 i->used = 0; | |
102 } | |
103 | |
104 n = recv(i->sock, i->buf + i->available, sizeof(i->buf) - i->available, 0); | |
105 | 101 |
106 if (n <= 0) | 102 if (n <= 0) |
107 return NULL; | 103 return NULL; |
108 | 104 |
109 i->available += n; | 105 i->available += n; |