]> granicus.if.org Git - libevent/commitdiff
test some primitives from http.c
authorNiels Provos <provos@gmail.com>
Tue, 29 Apr 2008 00:24:00 +0000 (00:24 +0000)
committerNiels Provos <provos@gmail.com>
Tue, 29 Apr 2008 00:24:00 +0000 (00:24 +0000)
svn:r739

evhttp.h
http.c
test/regress_http.c

index 54c504b5d5e73d6b82b1802f2c38805fe56a7d6e..01d6395a3c770ee4d68495d380038c6d5d503cbc 100644 (file)
--- a/evhttp.h
+++ b/evhttp.h
@@ -362,6 +362,17 @@ void evhttp_parse_query(const char *uri, struct evkeyvalq *);
  */
 char *evhttp_htmlescape(const char *html);
 
+/**
+ * Separate the host, port and path component from a URL.
+ *
+ * @param url the url for which to separate the components
+ * @param phost pointer in which to store the pointer to the host
+ * @param pport pointer in which to store the port number
+ * @param pfile pointer in which to store the pointer to the path
+ * @return 0 on success and -1 on failure
+ */
+int evhttp_hostportfile(char *url, char **phost, u_short *pport, char **pfile);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/http.c b/http.c
index a0df7712b56d52c676772757cafd3006e82e7e06..1695d610973726fad6408a72b68af7165541be45 100644 (file)
--- a/http.c
+++ b/http.c
@@ -465,7 +465,7 @@ evhttp_make_header(struct evhttp_connection *evcon, struct evhttp_request *req)
        }
 }
 
-/* Separated host, port and file from URI */
+/* Separate host, port and file from URI */
 
 int
 evhttp_hostportfile(char *url, char **phost, u_short *pport, char **pfile)
index c25cd3c6bb4b304bf322abe2826ceae98141e235..75a5bbf03b0038107fec6c4db06792fefc4a3141 100644 (file)
@@ -1384,9 +1384,56 @@ http_connection_retry(void)
        fprintf(stdout, "OK\n");
 }
 
+static void
+http_primitives(void)
+{
+       char *escaped;
+       char *host = NULL, *path = NULL;
+       unsigned short port = 0;
+       fprintf(stdout, "Testing HTTP Primitives: ");
+
+       escaped = evhttp_htmlescape("<script>");
+       if (strcmp(escaped, "&lt;script&gt;"))
+               goto failed;
+       free(escaped);
+
+       escaped = evhttp_htmlescape("\"\'&");
+       if (strcmp(escaped, "&quot;&#039;&amp;"))
+               goto failed;
+       free(escaped);
+
+       if (evhttp_hostportfile("fto://some", NULL, NULL, NULL) != -1)
+               goto failed;
+
+       if (evhttp_hostportfile("http://www.foo.com/path.html",
+               &host, &port, &path) == -1)
+               goto failed;
+
+       if (strcmp(host, "www.foo.com") || port != 80 ||
+           strcmp(path, "/path.html"))
+               goto failed;
+
+       if (evhttp_hostportfile("http://foo.com:8080",
+               &host, &port, &path) == -1)
+               goto failed;
+
+       if (strcmp(host, "foo.com") || port != 8080 || strcmp(path, "/"))
+               goto failed;
+
+       fprintf(stdout, "OK\n");
+
+       return;
+
+failed:
+       fprintf(stdout, "FAILED\n");
+       exit(1);
+}
+
 void
 http_suite(void)
 {
+       http_primitives();
+
        http_base_test();
        http_bad_header_test();
        http_basic_test();