Commit 4f93bef3 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Function to receive a HTTP header, as request or response.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@2680 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent b031becf
......@@ -27,6 +27,7 @@
*/
#include <poll.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
......@@ -44,8 +45,103 @@ struct http {
#define HTTP_MAGIC 0x2f02169c
int fd;
int client;
int timeout;
int nrxbuf;
char *rxbuf;
char *req;
char *resp;
};
/**********************************************************************
* Receive a HTTP protocol header
*/
static void
http_rxhdr(struct http *hp)
{
int l, n, i;
struct pollfd pfd[1];
char *p;
CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
hp->rxbuf = malloc(hp->nrxbuf); /* XXX */
AN(hp->rxbuf);
l = 0;
while (1) {
pfd[0].fd = hp->fd;
pfd[0].events = POLLRDNORM;
pfd[0].revents = 0;
i = poll(pfd, 1, hp->timeout);
assert(i > 0);
assert(l < hp->nrxbuf);
n = read(hp->fd, hp->rxbuf + l, 1);
assert(n == 1);
l += n;
hp->rxbuf[l] = '\0';
assert(n > 0);
p = hp->rxbuf + l - 1;
i = 0;
for (i = 0; p > hp->rxbuf; p--) {
if (*p != '\n')
break;
if (p - 1 > hp->rxbuf && p[-1] == '\r')
p--;
if (++i == 2)
break;
}
if (i == 2)
break;
}
printf("<<<%s>>>\n", hp->rxbuf);
}
/**********************************************************************
* Receive a response
*/
static void
cmd_http_rxresp(char **av, void *priv)
{
struct http *hp;
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
AN(hp->client);
assert(!strcmp(av[0], "rxresponse"));
av++;
for(; *av != NULL; av++) {
fprintf(stderr, "Unknown http rxresp spec: %s\n", *av);
exit (1);
}
http_rxhdr(hp);
hp->resp = hp->rxbuf;
}
/**********************************************************************
* Receive a request
*/
static void
cmd_http_rxreq(char **av, void *priv)
{
struct http *hp;
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
AZ(hp->client);
assert(!strcmp(av[0], "rxreq"));
av++;
for(; *av != NULL; av++) {
fprintf(stderr, "Unknown http rxreq spec: %s\n", *av);
exit (1);
}
http_rxhdr(hp);
hp->req = hp->rxbuf;
}
/**********************************************************************
* Transmit a request
*/
......@@ -98,7 +194,7 @@ cmd_http_txreq(char **av, void *priv)
av++;
continue;
}
fprintf(stderr, "Unknown http spec: %s\n", *av);
fprintf(stderr, "Unknown http txreq spec: %s\n", *av);
exit (1);
}
if (dohdr == 0) {
......@@ -120,9 +216,9 @@ cmd_http_txreq(char **av, void *priv)
static struct cmds http_cmds[] = {
{ "txreq", cmd_http_txreq },
{ "rxreq", cmd_dump },
{ "rxreq", cmd_http_rxreq },
{ "txresponse", cmd_dump },
{ "rxresponse", cmd_dump },
{ "rxresponse", cmd_http_rxresp },
{ "expect", cmd_dump },
{ NULL, NULL }
};
......@@ -136,6 +232,8 @@ http_process(const char *spec, int sock, int client)
ALLOC_OBJ(hp, HTTP_MAGIC);
hp->fd = sock;
hp->client = client;
hp->timeout = 1000;
hp->nrxbuf = 8192;
s = strdup(spec + 1);
q = strchr(s, '\0');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment