diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index b80a65737..bd2bc0da3 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 fc4fd024d..6daae786a 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 *