diff options
author | David Troy <dave@popvox.com> | 2006-04-03 21:33:51 +0000 |
---|---|---|
committer | David Troy <dave@popvox.com> | 2006-04-03 21:33:51 +0000 |
commit | 4af218734ff2ac7da1a6bdd55fde1ea71848fee7 (patch) | |
tree | 868fdd00bbefac05f3e218d52b661bfa7287d3f5 /src/http.c | |
parent | 6a521bdca3afa676e110ba2bada7698f41504084 (diff) | |
download | astmanproxy-4af218734ff2ac7da1a6bdd55fde1ea71848fee7.tar.gz astmanproxy-4af218734ff2ac7da1a6bdd55fde1ea71848fee7.tar.xz astmanproxy-4af218734ff2ac7da1a6bdd55fde1ea71848fee7.zip |
git-svn-id: http://svncommunity.digium.com/svn/astmanproxy/branches/1.20pre@77 f02b47b9-160a-0410-81a6-dc3441afb0ec
Diffstat (limited to 'src/http.c')
-rw-r--r-- | src/http.c | 74 |
1 files changed, 50 insertions, 24 deletions
@@ -32,24 +32,43 @@ int ParseHTTPInput(char *buf, struct message *m) { return (m->hdrcount > 0); } -int BuildHTTPHeader(char *hdr) { +int HTTPHeader(struct mansession *s, char *status) { time_t t; struct tm tm; char date[80]; + char ctype[15], hdr[MAX_LEN]; time(&t); localtime_r(&t, &tm); strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", &tm); - sprintf(hdr, - "HTTP/1.1 200 OK\r\n" - "Date: %s\r\n" - "Content-Type: text/xml\r\n" - "Connection: close\r\n" - "Server: %s/%s\r\n\r\n", - date, PROXY_BANNER, PROXY_VERSION); + if ( !strcasecmp("xml", s->output->formatname) ) + sprintf(ctype, "text/xml"); + else + sprintf(ctype, "text/plain"); + + if (!strcmp("200 OK", status) ) + sprintf(hdr, + "HTTP/1.1 %s\r\n" + "Date: %s\r\n" + "Content-Type: %s\r\n" + "Connection: close\r\n" + "Server: %s/%s\r\n\r\n", status, + date, ctype, PROXY_BANNER, PROXY_VERSION); + else + sprintf(hdr, + "HTTP/1.1 %s\r\n" + "Date: %s\r\n" + "Status: %s\r\n" + "Server: %s/%s\r\n\r\n", status, date, status, PROXY_BANNER, PROXY_VERSION); + + pthread_mutex_lock(&s->lock); + s->inputcomplete = 1; + ast_carefulwrite(s->fd, hdr, strlen(hdr), s->writetimeout); + pthread_mutex_unlock(&s->lock); + debugmsg("http header: %s", hdr); return 0; } @@ -59,12 +78,12 @@ int _read(struct mansession *s, struct message *m) { /* Note: No single line may be longer than MAX_LEN/s->inbuf, as per get_input */ /* No HTTP Input may be longer than BUFSIZE */ - char line[MAX_LEN], method[10], formdata[MAX_LEN], header[MAX_LEN]; + char line[MAX_LEN], method[10], formdata[MAX_LEN], status[15]; int res, clength = 0; memset(method, 0, sizeof method); memset(formdata, 0, sizeof formdata); - memset(header, 0, sizeof header); + memset(status, 0, sizeof status); /* for http, don't do get_input forever */ for (;;) { @@ -87,11 +106,15 @@ int _read(struct mansession *s, struct message *m) { if (!*method) { if ( !strncmp(line,"POST",4) ) { strncpy(method, line, 4); - } else if ( !strncmp(line,"GET",3) && strlen(line)>14 ) { - /* GET / HTTP/1.1 ---- this is bad */ - /* GET /?Action=Ping&ActionID=Foo HTTP/1.1 */ - strncpy(method, line, 3); - memcpy(formdata, line+6, strstr(line, " HTTP")-line-6); + } else if ( !strncmp(line,"GET",3)) { + if ( strlen(line) > 14 ) { + /* GET / HTTP/1.1 ---- this is bad */ + /* GET /?Action=Ping&ActionID=Foo HTTP/1.1 */ + strncpy(method, line, 3); + memcpy(formdata, line+6, strstr(line, " HTTP")-line-6); + sprintf(status, "200 OK"); + } else + sprintf(status, "501 Not Implemented"); } } } else if (res == 0) { @@ -101,6 +124,7 @@ int _read(struct mansession *s, struct message *m) { strncpy(formdata, s->inbuf, clength); s->inlen = 0; pthread_mutex_unlock(&s->lock); + sprintf(status, "200 OK"); } } } @@ -108,17 +132,19 @@ int _read(struct mansession *s, struct message *m) { if (res < 0) break; - if (*method && *formdata) { - BuildHTTPHeader(header); - pthread_mutex_lock(&s->lock); - s->inputcomplete = 1; - ast_carefulwrite(s->fd, header, strlen(header), s->writetimeout); - pthread_mutex_unlock(&s->lock); - debugmsg("header: %s", header); + if (*status) { + HTTPHeader(s, status); /* now, let's transform and copy into a standard message block */ - res = ParseHTTPInput(formdata, m); - return res; + if (!strcmp("200 OK", status) ) { + res = ParseHTTPInput(formdata, m); + return res; + } else { + pthread_mutex_lock(&s->lock); + s->outputcomplete = 1; + pthread_mutex_unlock(&s->lock); + return 0; + } } } return -1; |