http-url.c revision d45ab3fff7c47f1719b9cd310228c0dac2bdd1b2
/* Copyright (c) 2012 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "str.h"
#include "strfuncs.h"
#include "net.h"
#include "uri-util.h"
#include "http-url.h"
/*
* HTTP URL parser
*/
struct http_url_parser {
struct uri_parser parser;
enum http_url_parse_flags flags;
unsigned int relative:1;
};
{
struct uri_authority auth;
const char *const *path;
int path_relative;
const char *part;
int ret;
/* RFC 2616 - Hypertext Transfer Protocol, Section 3.2:
*
* http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]
*
* Translated to RFC 3986:
*
* absolute-http-URL = "http:" "//" host [ ":" port ] path-absolute
* ["?" query] [ "#" fragment ]
* relative-http-ref = relative-http-part [ "?" query ] [ "#" fragment ]
* relative-http-part = "//" host [ ":" port ] path-abempty
* / path-absolute
* / path-noscheme
* / path-empty
*/
/* "http:" / "https:" */
const char *scheme;
return FALSE;
else if (ret > 0) {
return FALSE;
}
}
} else {
}
/* "//" host [ ":" port ] */
return FALSE;
if (ret > 0) {
Section 2.8.1:
{...} Senders MUST NOT include a userinfo subcomponent (and its "@"
delimiter) when transmitting an "http" URI in a message. Recipients
of HTTP messages that contain a URI reference SHOULD parse for the
existence of userinfo and treat its presence as an error, likely
indicating that the deprecated subcomponent is being used to
obscure the authority for the sake of phishing attacks.
*/
return FALSE;
}
} else if (!relative) {
return FALSE;
}
}
/* path-abempty / path-absolute / path-noscheme / path-empty */
return FALSE;
/* Relative URLs are only valid when we have a base URL */
if (relative) {
return FALSE;
}
}
/* Resolve path */
if (ret > 0) {
const char *p = pend - 1;
/* discard trailing segments of base path based on how many effective
leading '..' segments were found in the relative path.
*/
while (path_relative > 0 && p > pbegin) {
while (p > pbegin && *p != '/') p--;
if (p >= pbegin) {
pend = p;
}
if (p > pbegin) p--;
}
}
/* append relative path */
return FALSE;
}
path++;
}
}
/* [ "?" query ] */
return FALSE;
if (ret > 0) {
return FALSE;
}
/* [ "#" fragment ] */
return FALSE;
if (ret > 0) {
return FALSE;
}
return FALSE;
}
return FALSE;
}
return TRUE;
}
/* Public API */
{
struct http_url_parser url_parser;
/* base != NULL indicates whether relative URLs are allowed. However, certain
if (!http_url_do_parse(&url_parser)) {
return -1;
}
return 0;
}
/*
* HTTP URL construction
*/
{
/* scheme */
/* host:port */
/* assume IPv6 literal if starts with '['; avoid encoding */
else
} else if (url->have_host_ip) {
} else
i_unreached();
/* Older syntax of RFC 2616 requires this slash at all times for an
absolute URL
*/
} else {
}
/* query (pre-encoded) */
}
/* fragment */
}
}