commit 369f9d93ac8362c71cec2d319b4c94067d339160
parent c84cd7867f7b1828fad454f4a5bd7c8498d2f944
Author: Kyle Milz <krwmilz@gmail.com>
Date: Fri, 17 Sep 2021 20:11:44 +0000
move parse_url() to util.c in preparation
Diffstat:
M | http.c | | | 55 | ------------------------------------------------------- |
M | util.c | | | 47 | +++++++++++++++++++++++++++++++++++++++++++++++ |
M | util.h | | | 13 | ++++++++++--- |
3 files changed, 57 insertions(+), 58 deletions(-)
diff --git a/http.c b/http.c
@@ -34,13 +34,6 @@
#define SSL_BUF_SIZE 16 * 1024
-struct url {
- const char *orig_url;
- char *hostname;
- char *port;
- char *document;
-};
-
struct http_headers {
char ver[15]; /* Should be enough, RFC2616 3.1 */
int stat; /* 200, 404, etc */
@@ -50,54 +43,6 @@ struct http_headers {
/*
- * Try and parse dest_url[] as a url:
- * - ignore protocol if found
- * - hostname is either what's remaining or up to a colon or slash character
- * - if colon is found, port number is either what is remaining or up to slash
- * character
- * - if slash is found the rest of the string is a document path
- *
- * On success a struct url pointer is returned that the caller must
- * free, otherwise NULL returned.
- */
-static struct url *
-parse_url(const char dest_url[])
-{
- struct url *url;
- char *sep_url;
- char *proto_loc;
- char *reset;
-
- url = xmalloc(sizeof(struct url));
- url->port = "https";
- url->orig_url = dest_url;
- url->document = "";
-
- /* dest_url[] will be modified */
- sep_url = xstrdup(dest_url);
-
- /* Do not care if this is specified, https is assumed. */
- if ((proto_loc = strstr(dest_url, "://")))
- sep_url = proto_loc + strlen("://");
-
- url->hostname = sep_url;
-
- reset = strsep(&sep_url, ":");
- if (sep_url != NULL)
- url->port = sep_url;
- else
- sep_url = reset;
-
- strsep(&sep_url, "/");
- if (sep_url != NULL)
- /* / was found, set document path */
- url->document = sep_url;
-
- return url;
-}
-
-
-/*
* Takes a struct url and finds all addresses related to url->hostname.
* Then tries connecting to each of them, returning a connected socket on
* success.
diff --git a/util.c b/util.c
@@ -452,3 +452,50 @@ prep_fd(int fd, Boolean noblock)
# endif
}
}
+
+/*
+ * Try and parse dest_url[] as a url:
+ * - ignore protocol if found
+ * - hostname is either what's remaining or up to a colon or slash character
+ * - if colon is found, port number is either what is remaining or up to slash
+ * character
+ * - if slash is found the rest of the string is a document path
+ *
+ * On success a struct url pointer is returned that the caller must
+ * free, otherwise NULL returned.
+ */
+struct url *
+parse_url(const char dest_url[])
+{
+ struct url *url;
+ char *sep_url;
+ char *proto_loc;
+ char *reset;
+
+ url = xmalloc(sizeof(struct url));
+ url->port = "https";
+ url->orig_url = dest_url;
+ url->document = "";
+
+ /* dest_url[] will be modified */
+ sep_url = xstrdup(dest_url);
+
+ /* Do not care if this is specified, https is assumed. */
+ if ((proto_loc = strstr(dest_url, "://")))
+ sep_url = proto_loc + strlen("://");
+
+ url->hostname = sep_url;
+
+ reset = strsep(&sep_url, ":");
+ if (sep_url != NULL)
+ url->port = sep_url;
+ else
+ sep_url = reset;
+
+ strsep(&sep_url, "/");
+ if (sep_url != NULL)
+ /* / was found, set document path */
+ url->document = sep_url;
+
+ return url;
+}
diff --git a/util.h b/util.h
@@ -1,13 +1,18 @@
-#include <stdio.h> /* FILE */
-
+#include <stdio.h> /* FILE */
#include <X11/Intrinsic.h> /* Boolean */
#include "data.h" /* struct bitmap, struct avl */
-
#define ROUNDUP(x,y) (((x)+(y)-1)/(y))
+struct url {
+ const char *orig_url;
+ char *hostname;
+ char *port;
+ char *document;
+};
+
void alloc_bitmap(struct bitmap *);
struct avl *avladd(const char *, size_t, struct avl **, size_t);
@@ -25,6 +30,8 @@ void *xrealloc(void *, size_t);
char *xstrdup(const char *);
char *xmemdup(const char *, size_t);
+struct url *parse_url(const char[]);
+
unsigned long num(FILE *, int);
long snum(FILE *, int);