From 7a0fcf2fc5fe974eb518b3eb052bae7c884f24e5 Mon Sep 17 00:00:00 2001
From: Andy Green <andy.green@linaro.org>
Date: Thu, 14 Jan 2016 13:39:02 +0800
Subject: [PATCH] parse_uri

Signed-off-by: Andy Green <andy.green@linaro.org>
---
 lib/libwebsockets.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 lib/libwebsockets.h |  3 +++
 2 files changed, 45 insertions(+)

diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c
index b80a6573..bd2bc0da 100644
--- a/lib/libwebsockets.c
+++ b/lib/libwebsockets.c
@@ -1096,6 +1096,48 @@ lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len)
 	return 0;
 }
 
+LWS_VISIBLE LWS_EXTERN int
+lws_parse_uri(char *p, const char **prot, const char **ads, int *port, const char **path)
+{
+	const char *end;
+	static const char *slash = "/";
+
+	/* cut up the location into address, port and path */
+	*prot = p;
+	while (*p && (*p != ':' || p[1] != '/' || p[2] != '/'))
+		p++;
+	if (!*p) {
+		end = p;
+		p = (char *)*prot;
+		*prot = end;
+	} else {
+		*p = '\0';
+		p += 3;
+	}
+	*ads = p;
+	if (!strcmp(*prot, "http") || !strcmp(*prot, "ws"))
+		*port = 80;
+	else if (!strcmp(*prot, "https") || !strcmp(*prot, "wss"))
+		*port = 443;
+	
+	while (*p && *p != ':' && *p != '/')
+		p++;
+	if (*p == ':') {
+		*p++ = '\0';
+		*port = atoi(p);
+		while (*p && *p != '/')
+			p++;
+	}
+	*path = slash;
+	if (*p) {
+		*p++ = '\0';
+		if (*p)
+			*path = p;
+	}
+
+	return 0;
+}
+
 #ifdef LWS_NO_EXTENSIONS
 
 /* we need to provide dummy callbacks for internal exts
diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h
index fc4fd024..6daae786 100644
--- a/lib/libwebsockets.h
+++ b/lib/libwebsockets.h
@@ -1661,6 +1661,9 @@ lws_b64_decode_string(const char *in, char *out, int out_size);
 LWS_VISIBLE LWS_EXTERN const char *
 lws_get_library_version(void);
 
+LWS_VISIBLE LWS_EXTERN int
+lws_parse_uri(char *p, const char **prot, const char **ads, int *port, const char **path);
+
 /*
  *  Access to http headers
  *
-- 
GitLab