diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c
index b80a657377e00c70837130307d737d709941663b..bd2bc0da365f2d1faffb4dffb0cae65555712338 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 fc4fd024d4ecc1aa6ba756b6949b1f0bc392a9d7..6daae786ae26dfe6e864a76e22ff1a4912081497 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
  *