uri = htx_sl_req_uri(sl);
if (sl->flags & HTX_SL_F_NORMALIZED_URI) {
- uri = http_get_path(uri);
+ struct http_uri_parser parser = http_uri_parser_init(uri);
+ uri = http_parse_path(&parser);
if (unlikely(!uri.len)) {
if (sl->info.req.meth == HTTP_METH_OPTIONS)
uri = ist("*");
URI_PARSER_STATE_BEFORE = 0,
URI_PARSER_STATE_SCHEME_DONE,
URI_PARSER_STATE_AUTHORITY_DONE,
+ URI_PARSER_STATE_PATH_DONE,
};
/* HTTP URI format as described in rfc 7230 5.3.
const char *http_get_reason(unsigned int status);
struct ist http_parse_scheme(struct http_uri_parser *parser);
struct ist http_parse_authority(struct http_uri_parser *parser, int no_userinfo);
-struct ist http_get_path(const struct ist uri);
+struct ist http_parse_path(struct http_uri_parser *parser);
int http_header_match2(const char *hdr, const char *end,
const char *name, int len);
char *http_find_hdr_value_end(char *s, const char *e);
uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
if (s->be->lbprm.arg_opt1 & 2) {
- uri = http_get_path(uri);
+ struct http_uri_parser parser =
+ http_uri_parser_init(uri);
+
+ uri = http_parse_path(&parser);
if (!isttest(uri))
uri = ist("");
}
struct ist path;
unsigned long long len = 0;
int32_t pos;
+ struct http_uri_parser parser;
/* Check stack size. */
if (!lua_checkstack(L, 3))
return 0;
lua_settable(L, -3);
- path = http_get_path(htx_sl_req_uri(sl));
+ parser = http_uri_parser_init(htx_sl_req_uri(sl));
+ path = http_parse_path(&parser);
if (isttest(path)) {
char *p, *q, *end;
/* Parse the URI from the given transaction (which is assumed to be in request
* phase) and look for the "/" beginning the PATH. If not found, ist2(0,0) is
* returned. Otherwise the pointer and length are returned.
+ *
+ * <parser> must have been initialized via http_uri_parser_init. See the
+ * related http_uri_parser documentation for the specific API usage.
*/
-struct ist http_get_path(const struct ist uri)
+struct ist http_parse_path(struct http_uri_parser *parser)
{
const char *ptr, *end;
- if (!uri.len)
+ if (parser->state >= URI_PARSER_STATE_PATH_DONE)
goto not_found;
- ptr = uri.ptr;
- end = ptr + uri.len;
+ if (parser->format == URI_PARSER_FORMAT_EMPTY ||
+ parser->format == URI_PARSER_FORMAT_ASTERISK) {
+ goto not_found;
+ }
+
+ ptr = istptr(parser->uri);
+ end = istend(parser->uri);
- /* RFC7230, par. 2.7 :
- * Request-URI = "*" | absuri | abspath | authority
+ /* If the uri is in absolute-path format, first skip the scheme and
+ * authority parts. No scheme will be found if the uri is in authority
+ * format, which indicates that the path won't be present.
*/
+ if (parser->format == URI_PARSER_FORMAT_ABSURI_OR_AUTHORITY) {
+ if (parser->state < URI_PARSER_STATE_SCHEME_DONE) {
+ /* If no scheme found, uri is in authority format. No
+ * path is present.
+ */
+ if (!isttest(http_parse_scheme(parser)))
+ goto not_found;
+ }
- if (*ptr == '*')
- goto not_found;
+ if (parser->state < URI_PARSER_STATE_AUTHORITY_DONE)
+ http_parse_authority(parser, 1);
- if (isalpha((unsigned char)*ptr)) {
- /* this is a scheme as described by RFC3986, par. 3.1 */
- ptr++;
- while (ptr < end &&
- (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
- ptr++;
- /* skip '://' */
- if (ptr == end || *ptr++ != ':')
- goto not_found;
- if (ptr == end || *ptr++ != '/')
- goto not_found;
- if (ptr == end || *ptr++ != '/')
+ ptr = istptr(parser->uri);
+
+ if (ptr == end)
goto not_found;
}
- /* skip [user[:passwd]@]host[:[port]] */
-
- while (ptr < end && *ptr != '/')
- ptr++;
-
- if (ptr == end)
- goto not_found;
- /* OK, we got the '/' ! */
+ parser->state = URI_PARSER_STATE_PATH_DONE;
return ist2(ptr, end - ptr);
not_found:
+ parser->state = URI_PARSER_STATE_PATH_DONE;
return IST_NULL;
}
switch ((enum act_normalize_uri) rule->action) {
case ACT_NORMALIZE_URI_PATH_MERGE_SLASHES: {
- const struct ist path = http_get_path(uri);
+ struct http_uri_parser parser = http_uri_parser_init(uri);
+ const struct ist path = http_parse_path(&parser);
struct ist newpath = ist2(replace->area, replace->size);
if (!isttest(path))
break;
}
case ACT_NORMALIZE_URI_PATH_STRIP_DOT: {
- const struct ist path = http_get_path(uri);
+ struct http_uri_parser parser = http_uri_parser_init(uri);
+ const struct ist path = http_parse_path(&parser);
struct ist newpath = ist2(replace->area, replace->size);
if (!isttest(path))
}
case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT:
case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL: {
- const struct ist path = http_get_path(uri);
+ struct http_uri_parser parser = http_uri_parser_init(uri);
+ const struct ist path = http_parse_path(&parser);
struct ist newpath = ist2(replace->area, replace->size);
if (!isttest(path))
break;
}
case ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME: {
- const struct ist path = http_get_path(uri);
+ struct http_uri_parser parser = http_uri_parser_init(uri);
+ const struct ist path = http_parse_path(&parser);
struct ist newquery = ist2(replace->area, replace->size);
if (!isttest(path))
}
case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE:
case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT: {
- const struct ist path = http_get_path(uri);
+ struct http_uri_parser parser = http_uri_parser_init(uri);
+ const struct ist path = http_parse_path(&parser);
struct ist newpath = ist2(replace->area, replace->size);
if (!isttest(path))
}
case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED:
case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT: {
- const struct ist path = http_get_path(uri);
+ struct http_uri_parser parser = http_uri_parser_init(uri);
+ const struct ist path = http_parse_path(&parser);
struct ist newpath = ist2(replace->area, replace->size);
if (!isttest(path))
break;
}
case ACT_NORMALIZE_URI_FRAGMENT_STRIP: {
- const struct ist path = http_get_path(uri);
+ struct http_uri_parser parser = http_uri_parser_init(uri);
+ const struct ist path = http_parse_path(&parser);
struct ist newpath = ist2(replace->area, replace->size);
if (!isttest(path))
break;
}
case ACT_NORMALIZE_URI_FRAGMENT_ENCODE: {
- const struct ist path = http_get_path(uri);
+ struct http_uri_parser parser = http_uri_parser_init(uri);
+ const struct ist path = http_parse_path(&parser);
struct ist newpath = ist2(replace->area, replace->size);
if (!isttest(path))
goto fail_alloc;
uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
- if (rule->action == 1) // replace-path
- uri = iststop(http_get_path(uri), '?');
- else if (rule->action == 4) // replace-pathq
- uri = http_get_path(uri);
+ if (rule->action == 1) { // replace-path
+ struct http_uri_parser parser = http_uri_parser_init(uri);
+ uri = iststop(http_parse_path(&parser), '?');
+ }
+ else if (rule->action == 4) { // replace-pathq
+ struct http_uri_parser parser = http_uri_parser_init(uri);
+ uri = http_parse_path(&parser);
+ }
if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
goto leave;
if (unlikely(sess->fe->monitor_uri_len != 0)) {
const struct ist monitor_uri = ist2(sess->fe->monitor_uri,
sess->fe->monitor_uri_len);
+ struct http_uri_parser parser = http_uri_parser_init(htx_sl_req_uri(sl));
if ((istptr(monitor_uri)[0] == '/' &&
- isteq(http_get_path(htx_sl_req_uri(sl)), monitor_uri)) ||
+ isteq(http_parse_path(&parser), monitor_uri)) ||
isteq(htx_sl_req_uri(sl), monitor_uri)) {
/*
* We have found the monitor URI
if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
struct htx_sl *sl;
struct ist uri, path;
+ struct http_uri_parser parser = http_uri_parser_init(uri);
if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
if (!(s->flags & SF_ERR_MASK))
}
sl = http_get_stline(htx);
uri = htx_sl_req_uri(sl);
- path = http_get_path(uri);
+ path = http_parse_path(&parser);
if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
goto return_bad_req;
case REDIRECT_TYPE_SCHEME: {
struct http_hdr_ctx ctx;
struct ist path, host;
+ struct http_uri_parser parser;
host = ist("");
ctx.blk = NULL;
host = ctx.value;
sl = http_get_stline(htx);
- path = http_get_path(htx_sl_req_uri(sl));
+ parser = http_uri_parser_init(htx_sl_req_uri(sl));
+ path = http_parse_path(&parser);
/* build message using path */
if (isttest(path)) {
if (rule->flags & REDIRECT_FLAG_DROP_QS) {
case REDIRECT_TYPE_PREFIX: {
struct ist path;
+ struct http_uri_parser parser;
sl = http_get_stline(htx);
- path = http_get_path(htx_sl_req_uri(sl));
+ parser = http_uri_parser_init(htx_sl_req_uri(sl));
+ path = http_parse_path(&parser);
/* build message using path */
if (isttest(path)) {
if (rule->flags & REDIRECT_FLAG_DROP_QS) {
htx = htxbuf(&s->req.buf);
sl = http_get_stline(htx);
uri = htx_sl_req_uri(sl);
- if (*uri_auth->uri_prefix == '/')
- uri = http_get_path(uri);
+ if (*uri_auth->uri_prefix == '/') {
+ struct http_uri_parser parser = http_uri_parser_init(uri);
+ uri = http_parse_path(&parser);
+ }
/* check URI size */
if (uri_auth->uri_len > uri.len)
struct htx_sl *sl;
struct ist path, location;
unsigned int flags;
+ struct http_uri_parser parser;
/*
* Create the location
/* 2: add the request Path */
htx = htxbuf(&req->buf);
sl = http_get_stline(htx);
- path = http_get_path(htx_sl_req_uri(sl));
+ parser = http_uri_parser_init(htx_sl_req_uri(sl));
+ path = http_parse_path(&parser);
if (!isttest(path))
return;
struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
struct htx_sl *sl;
struct ist path;
+ struct http_uri_parser parser;
if (!htx)
return 0;
sl = http_get_stline(htx);
- path = http_get_path(htx_sl_req_uri(sl));
+ parser = http_uri_parser_init(htx_sl_req_uri(sl));
if (kw[4] == 'q' && (kw[0] == 'p' || kw[0] == 'b')) // pathq or baseq
- path = http_get_path(htx_sl_req_uri(sl));
+ path = http_parse_path(&parser);
else
- path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
+ path = iststop(http_parse_path(&parser), '?');
if (!isttest(path))
return 0;
struct buffer *temp;
struct http_hdr_ctx ctx;
struct ist path;
+ struct http_uri_parser parser;
if (!htx)
return 0;
/* now retrieve the path */
sl = http_get_stline(htx);
- path = http_get_path(htx_sl_req_uri(sl));
+ parser = http_uri_parser_init(htx_sl_req_uri(sl));
+ path = http_parse_path(&parser);
if (isttest(path)) {
size_t len;
struct http_hdr_ctx ctx;
struct ist path;
unsigned int hash = 0;
+ struct http_uri_parser parser;
if (!htx)
return 0;
/* now retrieve the path */
sl = http_get_stline(htx);
- path = http_get_path(htx_sl_req_uri(sl));
+ parser = http_uri_parser_init(htx_sl_req_uri(sl));
+ path = http_parse_path(&parser);
if (isttest(path)) {
size_t len;
struct http_txn *txn;
struct ist path;
const char *ptr;
+ struct http_uri_parser parser;
if (!smp->strm)
return 0;
ptr++;
path.len = ptr - path.ptr;
- path = http_get_path(path);
+ parser = http_uri_parser_init(path);
+ path = http_parse_path(&parser);
if (!isttest(path))
return 0;
struct htx_sl *sl;
struct ist path;
unsigned int hash = 0;
+ struct http_uri_parser parser;
if (!htx)
return 0;
/* now retrieve the path */
sl = http_get_stline(htx);
- path = http_get_path(htx_sl_req_uri(sl));
+ parser = http_uri_parser_init(htx_sl_req_uri(sl));
+ path = http_parse_path(&parser);
if (path.len && *(path.ptr) == '/') {
while (path.len--)
hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
struct htx_sl *sl = http_get_stline(htx);
struct ist meth, uri, vsn, p;
size_t plen = 0;
+ struct http_uri_parser parser;
if (!sl)
return 0;
uri = htx_sl_req_uri(sl);
- p = http_get_path(uri);
+ parser = http_uri_parser_init(uri);
+ p = http_parse_path(&parser);
if (!isttest(p))
p = uri;
if (with_qs)
vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
/* reconstruct uri without port */
- path = http_get_path(uri);
+ path = http_parse_path(&parser);
chunk_istcat(temp, scheme);
chunk_istcat(temp, host);
chunk_istcat(temp, path);
struct timeval tv;
struct strm_logs tmp_strm_log;
struct ist path;
+ struct http_uri_parser parser;
/* FIXME: let's limit ourselves to frontend logging for now. */
path = ist2(uri, spc - uri);
// extract relative path without query params from url
- path = iststop(http_get_path(path), '?');
+ parser = http_uri_parser_init(path);
+ path = iststop(http_parse_path(&parser), '?');
if (!txn || !txn->uri || nspaces == 0) {
chunk.area = "<BADREQ>";
chunk.data = strlen("<BADREQ>");
#endif
if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
/* one of scriptname, pathinfo or query_string is no set */
- struct ist path = http_get_path(params->uri);
+ struct http_uri_parser parser = http_uri_parser_init(params->uri);
+ struct ist path = http_parse_path(&parser);
int len;
/* No scrit_name set but no valid path ==> error */