webserver update from Kate

git-svn-id: http://svn.code.sf.net/p/nuttx/code/trunk@5164 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2012-09-18 23:31:35 +00:00
parent b50f2a7834
commit e592dcedf2
1 changed files with 41 additions and 23 deletions

View File

@ -321,10 +321,11 @@ static int send_chunk(struct httpd_state *pstate, const char *buf, int len)
return OK; return OK;
} }
static int send_headers(struct httpd_state *pstate, int status) static int send_headers(struct httpd_state *pstate, int status, int len)
{ {
const char *mime; const char *mime;
const char *ptr; const char *ptr;
char cl[32];
char s[128]; char s[128];
int i; int i;
@ -367,6 +368,11 @@ static int send_headers(struct httpd_state *pstate, int status)
} }
} }
if (len >= 0)
{
(void) snprintf(cl, sizeof cl, "Content-Length: %d\r\n", len);
}
i = snprintf(s, sizeof s, i = snprintf(s, sizeof s,
"HTTP/1.0 %d %s\r\n" "HTTP/1.0 %d %s\r\n"
#ifndef CONFIG_NETUTILS_HTTPD_SERVERHEADER_DISABLE #ifndef CONFIG_NETUTILS_HTTPD_SERVERHEADER_DISABLE
@ -374,10 +380,12 @@ static int send_headers(struct httpd_state *pstate, int status)
#endif #endif
"Connection: close\r\n" "Connection: close\r\n"
"Content-type: %s\r\n" "Content-type: %s\r\n"
"%s"
"\r\n", "\r\n",
status, status,
status >= 400 ? "Error" : "OK", status >= 400 ? "Error" : "OK",
mime); mime,
len >= 0 ? cl : "");
return send_chunk(pstate, s, i); return send_chunk(pstate, s, i);
} }
@ -385,6 +393,7 @@ static int send_headers(struct httpd_state *pstate, int status)
static int httpd_senderror(struct httpd_state *pstate, int status) static int httpd_senderror(struct httpd_state *pstate, int status)
{ {
int ret; int ret;
char msg[10 + 1];
nvdbg("[%d] sending error '%d'\n", pstate->ht_sockfd, status); nvdbg("[%d] sending error '%d'\n", pstate->ht_sockfd, status);
@ -397,18 +406,18 @@ static int httpd_senderror(struct httpd_state *pstate, int status)
"%s/%d.html", "%s/%d.html",
CONFIG_NETUTILS_HTTPD_ERRPATH, status); CONFIG_NETUTILS_HTTPD_ERRPATH, status);
if (send_headers(pstate, status) != OK) ret = httpd_openindex(pstate);
if (send_headers(pstate, status, ret == OK ? pstate->ht_file.len : sizeof msg - 1) != OK)
{ {
return ERROR; return ERROR;
} }
if (httpd_openindex(pstate) != OK) if (ret != OK)
{ {
char s[10 + 1]; (void) snprintf(msg, sizeof msg, "Error %d\n", status);
(void) snprintf(s, sizeof s, "Error %d\n", status); ret = send_chunk(pstate, msg, sizeof msg - 1);
ret = send_chunk(pstate, s, sizeof s - 1);
} }
else else
{ {
@ -455,25 +464,34 @@ static int httpd_sendfile(struct httpd_state *pstate)
return httpd_senderror(pstate, 404); return httpd_senderror(pstate, 404);
} }
if (send_headers(pstate, 200) == OK)
{
#ifndef CONFIG_NETUTILS_HTTPD_SCRIPT_DISABLE #ifndef CONFIG_NETUTILS_HTTPD_SCRIPT_DISABLE
ptr = strchr(pstate->ht_filename, ISO_period); ptr = strchr(pstate->ht_filename, ISO_period);
if (ptr != NULL && if (ptr != NULL &&
strncmp(ptr, g_httpextensionshtml, strlen(g_httpextensionshtml)) == 0) strncmp(ptr, ".shtml", strlen(".shtml")) == 0)
{ {
ret = handle_script(pstate); if (send_headers(pstate, 200, -1) != OK)
{
goto done;
}
ret = handle_script(pstate);
goto done;
} }
else
#endif #endif
if (send_headers(pstate, pstate->ht_file.len == 0 ? 204 : 200, pstate->ht_file.len) != OK)
{ {
goto done;
}
#ifdef CONFIG_NETUTILS_HTTPD_SENDFILE #ifdef CONFIG_NETUTILS_HTTPD_SENDFILE
ret = httpd_sendfile_send(pstate->ht_sockfd, &pstate->ht_file); ret = httpd_sendfile_send(pstate->ht_sockfd, &pstate->ht_file);
#else #else
ret = send_chunk(pstate, pstate->ht_file.data, pstate->ht_file.len); ret = send_chunk(pstate, pstate->ht_file.data, pstate->ht_file.len);
#endif #endif
}
} done:
(void)httpd_close(&pstate->ht_file); (void)httpd_close(&pstate->ht_file);