+static const char *parse_path(php_http_url_t *url)
+{
+ size_t mb;
+ const char *tmp;
+ TSRMLS_FETCH_FROM_CTX(url->ts);
+
+ /* is there actually a path to parse? */
+ if (!*url->ptr) {
+ return url->ptr;
+ }
+ tmp = url->ptr;
+ url->path = &url->buffer[url->offset];
+
+ do {
+ switch (*url->ptr) {
+ case '#':
+ case '?':
+ case '\0':
+ /* did we have any path component ? */
+ if (tmp != url->ptr) {
+ url->buffer[url->offset++] = 0;
+ } else {
+ url->path = NULL;
+ }
+ return url->ptr;
+
+ case '%':
+ if (url->ptr[1] != '%' && (url->end - url->ptr <= 2 || !isxdigit(*(url->ptr+1)) || !isxdigit(*(url->ptr+2)))) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ "Failed to parse path; invalid percent encoding at pos %u in '%s'",
+ (unsigned) (url->ptr - tmp), tmp);
+ return NULL;
+ }
+ url->buffer[url->offset++] = *url->ptr++;
+ url->buffer[url->offset++] = *url->ptr++;
+ url->buffer[url->offset++] = *url->ptr;
+ break;
+
+ case '/': /* yeah, well */
+ case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
+ case '+': case ',': case ';': case '=': /* sub-delims */
+ case '-': case '.': case '_': case '~': /* unreserved */
+ case ':': case '@': /* pchar */
+ case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
+ case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
+ case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
+ case 'V': case 'W': case 'X': case 'Y': case 'Z':
+ case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
+ case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
+ case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
+ case 'v': case 'w': case 'x': case 'y': case 'z':
+ case '0': case '1': case '2': case '3': case '4': case '5': case '6':
+ case '7': case '8': case '9':
+ /* allowed */
+ url->buffer[url->offset++] = *url->ptr;
+ break;
+
+ default:
+ if (!(mb = parse_mb(url, PARSE_PATH, url->ptr, url->end, tmp, 0))) {
+ return NULL;
+ }
+ url->ptr += mb - 1;
+ }
+ } while (++url->ptr <= url->end);
+
+ return NULL;
+}
+
+static const char *parse_query(php_http_url_t *url)
+{
+ size_t mb;
+ const char *tmp = url->ptr + !!*url->ptr;
+ TSRMLS_FETCH_FROM_CTX(url->ts);
+
+ /* is there actually a query to parse ? */
+ if (!*url->ptr && *url->ptr != '?') {
+ return url->ptr;
+ }
+
+ /* skip initial '?' */
+ tmp = ++url->ptr;
+ url->query = &url->buffer[url->offset];
+
+ do {
+ switch (*url->ptr) {
+ case '#':
+ case '\0':
+ url->buffer[url->offset++] = 0;
+ return url->ptr;
+
+ case '%':
+ if (url->ptr[1] != '%' && (url->end - url->ptr <= 2 || !isxdigit(*(url->ptr+1)) || !isxdigit(*(url->ptr+2)))) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ "Failed to parse query; invalid percent encoding at pos %u in '%s'",
+ (unsigned) (url->ptr - tmp), tmp);
+ return NULL;
+ }
+ url->buffer[url->offset++] = *url->ptr++;
+ url->buffer[url->offset++] = *url->ptr++;
+ url->buffer[url->offset++] = *url->ptr;
+ break;
+
+ case '?': case '/': /* yeah, well */
+ case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
+ case '+': case ',': case ';': case '=': /* sub-delims */
+ case '-': case '.': case '_': case '~': /* unreserved */
+ case ':': case '@': /* pchar */
+ case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
+ case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
+ case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
+ case 'V': case 'W': case 'X': case 'Y': case 'Z':
+ case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
+ case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
+ case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
+ case 'v': case 'w': case 'x': case 'y': case 'z':
+ case '0': case '1': case '2': case '3': case '4': case '5': case '6':
+ case '7': case '8': case '9':
+ /* allowed */
+ url->buffer[url->offset++] = *url->ptr;
+ break;
+
+ default:
+ if (!(mb = parse_mb(url, PARSE_QUERY, url->ptr, url->end, tmp, 0))) {
+ return NULL;
+ }
+ url->ptr += mb - 1;
+ }
+ } while (++url->ptr <= url->end);
+
+ return NULL;
+}
+
+static const char *parse_fragment(php_http_url_t *url)
+{
+ size_t mb;
+ const char *tmp;
+ TSRMLS_FETCH_FROM_CTX(url->ts);
+
+ /* is there actually a fragment to parse */
+ if (!*url->ptr && *url->ptr != '#') {
+ return url->ptr;
+ }
+
+ /* skip initial '#' */
+ tmp = ++url->ptr;
+ url->fragment = &url->buffer[url->offset];
+
+ do {
+ switch (*url->ptr) {
+ case '\0':
+ url->buffer[url->offset++] = 0;
+ return url->ptr;
+
+ case '%':
+ if (url->ptr[1] != '%' && (url->end - url->ptr <= 2 || !isxdigit(*(url->ptr+1)) || !isxdigit(*(url->ptr+2)))) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ "Failed to parse fragment; invalid percent encoding at pos %u in '%s'",
+ (unsigned) (url->ptr - tmp), tmp);
+ return NULL;
+ }
+ url->buffer[url->offset++] = *url->ptr++;
+ url->buffer[url->offset++] = *url->ptr++;
+ url->buffer[url->offset++] = *url->ptr;
+ break;
+
+ case '?': case '/':
+ case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
+ case '+': case ',': case ';': case '=': /* sub-delims */
+ case '-': case '.': case '_': case '~': /* unreserved */
+ case ':': case '@': /* pchar */
+ case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
+ case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
+ case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
+ case 'V': case 'W': case 'X': case 'Y': case 'Z':
+ case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
+ case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
+ case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
+ case 'v': case 'w': case 'x': case 'y': case 'z':
+ case '0': case '1': case '2': case '3': case '4': case '5': case '6':
+ case '7': case '8': case '9':
+ /* allowed */
+ url->buffer[url->offset++] = *url->ptr;
+ break;
+
+ default:
+ if (!(mb = parse_mb(url, PARSE_FRAGMENT, url->ptr, url->end, tmp, 0))) {
+ return NULL;
+ }
+ url->ptr += mb - 1;
+ }
+ } while (++url->ptr <= url->end);
+
+ return NULL;
+}
+
+static const char *parse_hier(php_http_url_t *url)
+{
+ if (*url->ptr == '/') {
+ if (url->end - url->ptr > 1) {
+ if (*(url->ptr + 1) == '/') {
+ url->ptr += 2;
+ if (!(url->ptr = parse_authority(url))) {
+ return NULL;
+ }
+ }
+ }
+ }
+ return parse_path(url);
+}
+
+static const char *parse_scheme(php_http_url_t *url)
+{
+ size_t mb;
+ const char *tmp = url->ptr;
+
+ do {
+ switch (*url->ptr) {
+ case ':':
+ /* scheme delimiter */
+ url->scheme = &url->buffer[0];
+ url->buffer[url->offset++] = 0;
+ return ++url->ptr;
+
+ case '0': case '1': case '2': case '3': case '4': case '5': case '6':
+ case '7': case '8': case '9':
+ case '+': case '-': case '.':
+ if (url->ptr == tmp) {
+ return tmp;
+ }
+ /* no break */
+ case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
+ case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
+ case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
+ case 'V': case 'W': case 'X': case 'Y': case 'Z':
+ case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
+ case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
+ case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
+ case 'v': case 'w': case 'x': case 'y': case 'z':
+ /* scheme part */
+ url->buffer[url->offset++] = *url->ptr;
+ break;
+
+ default:
+ if (!(mb = parse_mb(url, PARSE_SCHEME, url->ptr, url->end, tmp, 1))) {
+ /* soft fail; parse path next */
+ return tmp;
+ }
+ url->ptr += mb - 1;
+ }
+ } while (++url->ptr != url->end);
+
+ return tmp;
+}
+
+struct parser_state {