Permalink
Browse files

Make ProtoCHttp objects cache the http length

This moves the code from ProxyClient to ProtoCHttp which is more
logical.
  • Loading branch information...
1 parent 1c3f979 commit d384d72e6e9d86878cb49c45de17bec5809907e4 @rosslagerwall committed Jun 25, 2012
Showing with 12 additions and 15 deletions.
  1. +2 −4 src/pclient.c
  2. +0 −1 src/pclient.h
  3. +8 −7 src/protochttp.c
  4. +2 −3 src/protochttp.h
View
@@ -58,7 +58,6 @@ pclient_new(struct event_base *base, char *location, char *path)
/* private */
client->n_written = 0;
- client->length = 0;
client->observers = NULL;
client->proto = pchttp_new();
client->state = PC_SEND;
@@ -130,16 +129,15 @@ pclient_read_cb(struct bufferevent *bev, void *ctx)
fwrite(out, 1, ret, client->sink);
fflush(client->sink);
client->n_written = ret;
- client->length = pchttp_get_length(client->proto);
- pclient_notify_all_data(client, client->n_written, client->length);
+ pclient_notify_all_data(client, client->n_written, client->proto->http_length);
free(out);
client->state = PC_RECV;
}
} else {
fwrite(buf, 1, n, client->sink);
fflush(client->sink);
client->n_written += n;
- pclient_notify_all_data(client, client->n_written, client->length);
+ pclient_notify_all_data(client, client->n_written, client->proto->http_length);
}
}
}
View
@@ -41,7 +41,6 @@ struct ProxyClient {
char *orig_path;
FILE *sink;
GList *observers;
- int length;
int n_written;
};
View
@@ -31,6 +31,8 @@ const char REQUEST[] = "GET %s HTTP/1.0\r\n"
"User-Agent: yummycacher\r\n"
"Host: archive.fedoraproject.org\r\n\r\n";
+void
+pchttp_parse_headers(struct ProtoCHttp *proto);
struct ProtoCHttp *
pchttp_new(void)
@@ -43,6 +45,8 @@ pchttp_new(void)
proto->buf[0] = '\0';
proto->bufsize = 0;
+ proto->http_length = 0;
+
return proto;
}
@@ -65,7 +69,7 @@ pchttp_add_data(struct ProtoCHttp *proto, char *data, int n, char **out)
if ((ptr = strstr(proto->buf, "\r\n\r\n")) != NULL) {
/* parse the headers */
ptr[0] = '\0';
-/// http_request_parse_headers(req);
+ pchttp_parse_headers(proto);
ptr += 4;
size_t count = proto->bufsize - (ptr - proto->buf);
@@ -76,21 +80,18 @@ pchttp_add_data(struct ProtoCHttp *proto, char *data, int n, char **out)
return 0;
}
-int
-pchttp_get_length(struct ProtoCHttp *proto)
+void
+pchttp_parse_headers(struct ProtoCHttp *proto)
{
char **lines = g_strsplit(proto->buf, "\r\n", 0);
char **lineptr = lines;
- int length = 0;
while (*lineptr) {
if (g_str_has_prefix(*lineptr, "Content-Length:"))
- sscanf(*lineptr, "Content-Length: %d", &length);
+ sscanf(*lineptr, "Content-Length: %d", &proto->http_length);
lineptr++;
}
g_strfreev(lines);
-
- return length;
}
char *
View
@@ -24,6 +24,8 @@ struct ProtoCHttp {
/* private */
char *buf;
int bufsize;
+
+ int http_length;
};
struct ProtoCHttp *
@@ -35,9 +37,6 @@ pchttp_free(struct ProtoCHttp *proto);
int
pchttp_add_data(struct ProtoCHttp *proto, char *data, int n, char **out);
-int
-pchttp_get_length(struct ProtoCHttp *proto);
-
char *
pchttp_get_request(struct ProtoCHttp *proto, char *location);

0 comments on commit d384d72

Please sign in to comment.