+static const char *parse_authority(struct parse_state *state)
+{
+ const char *tmp = state->ptr, *host = NULL;
+
+ do {
+ switch (*state->ptr) {
+ case '@':
+ /* userinfo delimiter */
+ if (host) {
+ php_error_docref(NULL, E_WARNING,
+ "Failed to parse userinfo; unexpected '@'");
+ return NULL;
+ }
+ host = state->ptr + 1;
+ if (tmp != state->ptr && SUCCESS != parse_userinfo(state, tmp)) {
+ return NULL;
+ }
+ tmp = state->ptr + 1;
+ break;
+
+ case '/':
+ case '?':
+ case '#':
+ case '\0':
+ EOD:
+ /* host delimiter */
+ if (tmp != state->ptr && SUCCESS != parse_hostinfo(state, tmp)) {
+ return NULL;
+ }
+ return state->ptr;
+ }
+ } while (++state->ptr <= state->end);
+
+ --state->ptr;
+ goto EOD;
+}
+
+static const char *parse_path(struct parse_state *state)
+{
+ size_t mb;
+ const char *tmp;
+
+ /* is there actually a path to parse? */
+ if (!*state->ptr) {
+ return state->ptr;
+ }
+ tmp = state->ptr;
+ state->url.path = &state->buffer[state->offset];
+
+ do {
+ switch (*state->ptr) {
+ case '#':
+ case '?':
+ goto done;
+
+ case '%':
+ if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
+ php_error_docref(NULL, E_WARNING,
+ "Failed to parse path; invalid percent encoding at pos %u in '%s'",
+ (unsigned) (state->ptr - tmp), tmp);
+ return NULL;
+ }
+ state->buffer[state->offset++] = *state->ptr++;
+ state->buffer[state->offset++] = *state->ptr++;
+ state->buffer[state->offset++] = *state->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 */
+ state->buffer[state->offset++] = *state->ptr;
+ break;
+
+ default:
+ if (!(mb = parse_mb(state, PARSE_PATH, state->ptr, state->end, tmp, 0))) {
+ return NULL;
+ }
+ state->ptr += mb - 1;
+ }
+ } while (++state->ptr < state->end);
+
+ done:
+ /* did we have any path component ? */
+ if (tmp != state->ptr) {
+ state->buffer[state->offset++] = 0;
+ } else {
+ state->url.path = NULL;
+ }
+ return state->ptr;
+}
+
+static const char *parse_query(struct parse_state *state)
+{
+ size_t mb;
+ const char *tmp = state->ptr + !!*state->ptr;
+
+ /* is there actually a query to parse? */
+ if (*state->ptr != '?') {
+ return state->ptr;
+ }
+
+ /* skip initial '?' */
+ tmp = ++state->ptr;
+ state->url.query = &state->buffer[state->offset];
+
+ while (state->ptr < state->end) {
+ switch (*state->ptr) {
+ case '#':
+ goto done;
+
+ case '%':
+ if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
+ php_error_docref(NULL, E_WARNING,
+ "Failed to parse query; invalid percent encoding at pos %u in '%s'",
+ (unsigned) (state->ptr - tmp), tmp);
+ return NULL;
+ }
+ state->buffer[state->offset++] = *state->ptr++;
+ state->buffer[state->offset++] = *state->ptr++;
+ state->buffer[state->offset++] = *state->ptr;
+ break;
+
+ case ']':
+ case '[':
+ if (state->flags & PHP_HTTP_URL_PARSE_TOPCT) {
+ state->buffer[state->offset++] = '%';
+ state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) >> 4];
+ state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) & 0xf];
+ break;
+ }
+ /* no 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 */
+ state->buffer[state->offset++] = *state->ptr;
+ break;
+
+ default:
+ if (!(mb = parse_mb(state, PARSE_QUERY, state->ptr, state->end, tmp, 0))) {
+ return NULL;
+ }
+ state->ptr += mb - 1;
+ }
+
+ ++state->ptr;
+ }
+
+ done:
+ state->buffer[state->offset++] = 0;
+ return state->ptr;
+}
+
+static const char *parse_fragment(struct parse_state *state)
+{
+ size_t mb;
+ const char *tmp;
+
+ /* is there actually a fragment to parse? */
+ if (*state->ptr != '#') {
+ return state->ptr;
+ }
+
+ /* skip initial '#' */
+ tmp = ++state->ptr;
+ state->url.fragment = &state->buffer[state->offset];
+
+ do {
+ switch (*state->ptr) {
+ case '%':
+ if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
+ php_error_docref(NULL, E_WARNING,
+ "Failed to parse fragment; invalid percent encoding at pos %u in '%s'",
+ (unsigned) (state->ptr - tmp), tmp);
+ return NULL;
+ }
+ state->buffer[state->offset++] = *state->ptr++;
+ state->buffer[state->offset++] = *state->ptr++;
+ state->buffer[state->offset++] = *state->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 */
+ state->buffer[state->offset++] = *state->ptr;
+ break;
+
+ default:
+ if (!(mb = parse_mb(state, PARSE_FRAGMENT, state->ptr, state->end, tmp, 0))) {
+ return NULL;
+ }
+ state->ptr += mb - 1;
+ }
+ } while (++state->ptr < state->end);
+
+ state->buffer[state->offset++] = 0;
+ return state->ptr;
+}
+
+static const char *parse_hier(struct parse_state *state)
+{
+ if (*state->ptr == '/') {
+ if (state->end - state->ptr > 1) {
+ if (*(state->ptr + 1) == '/') {
+ state->ptr += 2;
+ if (!(state->ptr = parse_authority(state))) {
+ return NULL;
+ }
+ }
+ }
+ }
+ return parse_path(state);
+}
+
+static const char *parse_scheme(struct parse_state *state)
+{
+ size_t mb;
+ const char *tmp = state->ptr;
+
+ do {
+ switch (*state->ptr) {
+ case ':':
+ /* scheme delimiter */
+ state->url.scheme = &state->buffer[0];
+ state->buffer[state->offset++] = 0;
+ return ++state->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 (state->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 */
+ state->buffer[state->offset++] = *state->ptr;
+ break;
+
+ default:
+ if (!(mb = parse_mb(state, PARSE_SCHEME, state->ptr, state->end, tmp, 1))) {
+ /* soft fail; parse path next */
+ return tmp;
+ }
+ state->ptr += mb - 1;
+ }
+ } while (++state->ptr != state->end);
+
+ return state->ptr = tmp;
+}
+
+php_http_url_t *php_http_url_parse(const char *str, size_t len, unsigned flags)
+{
+ size_t maxlen = 3 * len;
+ struct parse_state *state = ecalloc(1, sizeof(*state) + maxlen);
+
+ state->end = str + len;
+ state->ptr = str;
+ state->flags = flags;
+ state->maxlen = maxlen;
+
+ if (!parse_scheme(state)) {
+ php_error_docref(NULL, E_WARNING, "Failed to parse URL scheme: '%s'", state->ptr);
+ efree(state);
+ return NULL;
+ }
+
+ if (!parse_hier(state)) {
+ efree(state);
+ return NULL;
+ }
+
+ if (!parse_query(state)) {
+ php_error_docref(NULL, E_WARNING, "Failed to parse URL query: '%s'", state->ptr);
+ efree(state);
+ return NULL;
+ }
+
+ if (!parse_fragment(state)) {
+ php_error_docref(NULL, E_WARNING, "Failed to parse URL fragment: '%s'", state->ptr);
+ efree(state);
+ return NULL;
+ }
+
+ return (php_http_url_t *) state;
+}
+
+php_http_url_t *php_http_url_parse_authority(const char *str, size_t len, unsigned flags)
+{
+ size_t maxlen = 3 * len;
+ struct parse_state *state = ecalloc(1, sizeof(*state) + maxlen);
+
+ state->end = str + len;
+ state->ptr = str;
+ state->flags = flags;
+ state->maxlen = maxlen;
+ TSRMLS_SET_CTX(state->ts);
+
+ if (!(state->ptr = parse_authority(state))) {
+ efree(state);
+ return NULL;
+ }
+
+ if (state->ptr != state->end) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ "Failed to parse URL authority, unexpected character at pos %u in '%s'",
+ (unsigned) (state->ptr - str), str);
+ efree(state);
+ return NULL;
+ }
+
+ return (php_http_url_t *) state;
+}
+