summaryrefslogtreecommitdiff
path: root/http.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2010-11-09 10:14:32 -0500
committerNick Mathewson <nickm@torproject.org>2010-11-09 10:14:32 -0500
commita38140be18b764e98b899ed435b61ea2aa6447bc (patch)
tree71b438580dd0df7abe06cbd09d070d07957c7ebe /http.c
parent647e094ca2ff90da79bfc3081848f50583d2042c (diff)
downloadlibevent-a38140be18b764e98b899ed435b61ea2aa6447bc.tar.gz
Refactor http version parsing into a single function
Based on a suggestion by Chris Davis to make evhttp_parse_response_line tolerate odd versions too.
Diffstat (limited to 'http.c')
-rw-r--r--http.c47
1 files changed, 19 insertions, 28 deletions
diff --git a/http.c b/http.c
index ac5e547a..7b50eec9 100644
--- a/http.c
+++ b/http.c
@@ -1312,6 +1312,22 @@ evhttp_valid_response_code(int code)
return (1);
}
+static int
+evhttp_parse_http_version(const char *version, struct evhttp_request *req)
+{
+ int major, minor;
+ char ch;
+ int n = sscanf(version, "HTTP/%d.%d%c", &major, &minor, &ch);
+ if (n > 2 || major > 1) {
+ event_debug(("%s: bad version %s on message %p from %s",
+ __func__, version, req, req->remote_host));
+ return (-1);
+ }
+ req->major = major;
+ req->minor = minor;
+ return (0);
+}
+
/* Parses the status line of a web server */
static int
@@ -1328,17 +1344,8 @@ evhttp_parse_response_line(struct evhttp_request *req, char *line)
if (line != NULL)
readable = line;
- if (strcmp(protocol, "HTTP/1.0") == 0) {
- req->major = 1;
- req->minor = 0;
- } else if (strcmp(protocol, "HTTP/1.1") == 0) {
- req->major = 1;
- req->minor = 1;
- } else {
- event_debug(("%s: bad protocol \"%s\"",
- __func__, protocol));
+ if (evhttp_parse_http_version(protocol, req) < 0)
return (-1);
- }
req->response_code = atoi(number);
if (!evhttp_valid_response_code(req->response_code)) {
@@ -1392,24 +1399,8 @@ evhttp_parse_request_line(struct evhttp_request *req, char *line)
return (-1);
}
- if (strcmp(version, "HTTP/1.0") == 0) {
- req->major = 1;
- req->minor = 0;
- } else if (strcmp(version, "HTTP/1.1") == 0) {
- req->major = 1;
- req->minor = 1;
- } else {
- int major, minor;
- char ch;
- int n = sscanf(version, "HTTP/%d.%d%c", &major, &minor, &ch);
- if (n > 2 || major > 1) {
- event_debug(("%s: bad version %s on request %p from %s",
- __func__, version, req, req->remote_host));
- return (-1);
- }
- req->major = major;
- req->minor = minor;
- }
+ if (evhttp_parse_http_version(version, req) < 0)
+ return (-1);
if ((req->uri = mm_strdup(uri)) == NULL) {
event_debug(("%s: mm_strdup", __func__));