Clean up html_send_file ()

- Make function return from one place
 - Move inbuf to the heap
This commit is contained in:
Mukund Sivaraman 2009-10-02 13:01:32 +05:30
parent 21c8d7a7ed
commit 323a4d0147

View File

@ -103,42 +103,42 @@ static char *lookup_variable (struct conn_s *connptr, const char *varname)
return (data); return (data);
} }
#define HTML_BUFSIZE 4096
/* /*
* Send an already-opened file to the client with variable substitution. * Send an already-opened file to the client with variable substitution.
*/ */
int send_html_file (FILE * infile, struct conn_s *connptr) int
send_html_file (FILE *infile, struct conn_s *connptr)
{ {
char inbuf[HTML_BUFSIZE], *varstart = NULL, *p; char *inbuf;
char *varstart = NULL;
char *p;
const char *varval; const char *varval;
int in_variable = 0, writeret; int in_variable = 0;
int r = 0;
while (fgets (inbuf, HTML_BUFSIZE, infile) != NULL) { inbuf = (char *) safemalloc (4096);
while (fgets (inbuf, 4096, infile) != NULL) {
for (p = inbuf; *p; p++) { for (p = inbuf; *p; p++) {
switch (*p) { switch (*p) {
case '}': case '}':
if (in_variable) { if (in_variable) {
*p = '\0'; *p = '\0';
varval = varval = (const char *)
(const char *) lookup_variable (connptr,
lookup_variable (connptr, varstart); varstart);
if (!varval) if (!varval)
varval = "(unknown)"; varval = "(unknown)";
writeret = r = write_message (connptr->client_fd,
write_message (connptr->client_fd,
"%s", varval); "%s", varval);
if (writeret)
return (writeret);
in_variable = 0; in_variable = 0;
} else { } else {
writeret = r = write_message (connptr->client_fd,
write_message (connptr->client_fd,
"%c", *p); "%c", *p);
if (writeret)
return (writeret);
} }
break; break;
case '{': case '{':
/* a {{ will print a single {. If we are NOT /* a {{ will print a single {. If we are NOT
* already in a { variable, then proceed with * already in a { variable, then proceed with
@ -151,20 +151,27 @@ int send_html_file (FILE * infile, struct conn_s *connptr)
in_variable++; in_variable++;
} else } else
in_variable = 0; in_variable = 0;
default: default:
if (!in_variable) { if (!in_variable) {
writeret = r = write_message (connptr->client_fd,
write_message (connptr->client_fd,
"%c", *p); "%c", *p);
if (writeret)
return (writeret);
} }
} }
if (r)
break;
} }
if (r)
break;
in_variable = 0; in_variable = 0;
} }
return (0);
safefree (inbuf);
return r;
} }
int send_http_headers (struct conn_s *connptr, int code, const char *message) int send_http_headers (struct conn_s *connptr, int code, const char *message)