2008-08-09 15:20:12 +00:00
|
|
|
|
2008-11-16 20:33:53 +00:00
|
|
|
#include <lighttpd/base.h>
|
2009-01-01 15:44:42 +00:00
|
|
|
#include <lighttpd/url_parser.h>
|
2008-08-09 15:20:12 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
%%{
|
|
|
|
machine url_parser;
|
|
|
|
|
|
|
|
action mark { mark = fpc; }
|
|
|
|
action mark_host { host_mark = fpc; }
|
|
|
|
|
|
|
|
action save_host {
|
|
|
|
g_string_truncate(uri->host, 0);
|
|
|
|
g_string_append_len(uri->host, host_mark, fpc - host_mark);
|
|
|
|
g_string_ascii_down(uri->host);
|
|
|
|
}
|
|
|
|
action save_authority {
|
|
|
|
g_string_truncate(uri->authority, 0);
|
|
|
|
g_string_append_len(uri->authority, mark, fpc - mark);
|
|
|
|
g_string_ascii_down(uri->authority);
|
|
|
|
}
|
|
|
|
action save_path {
|
|
|
|
g_string_append_len(uri->path, mark, fpc - mark);
|
2009-10-14 17:29:08 +00:00
|
|
|
g_string_append_len(uri->raw_path, mark, fpc - mark);
|
2008-08-09 15:20:12 +00:00
|
|
|
}
|
|
|
|
action save_query {
|
|
|
|
g_string_append_len(uri->query, mark, fpc - mark);
|
|
|
|
}
|
2009-09-13 15:45:41 +00:00
|
|
|
action save_scheme {
|
|
|
|
g_string_append_len(uri->scheme, mark, fpc - mark);
|
|
|
|
}
|
2008-08-09 15:20:12 +00:00
|
|
|
|
|
|
|
pct_encoded = "%" xdigit xdigit;
|
|
|
|
|
|
|
|
gen_delims = ":" | "/" | "?" | "#" | "[" | "]" | "@";
|
|
|
|
sub_delims = "!" | "$" | "&" | "'" | "(" | ")"
|
|
|
|
| "*" | "+" | "," | ";" | "=";
|
|
|
|
|
|
|
|
reserved = gen_delims | sub_delims;
|
|
|
|
unreserved = alpha | digit | "-" | "." | "_" | "~";
|
|
|
|
|
2009-01-13 12:48:03 +00:00
|
|
|
# many clients don't encode these, e.g. curl, wget, ...
|
|
|
|
delims = "<" | ">" | "#" | "%" | '"';
|
|
|
|
unwise = " " | "{" | "}" | "|" | "\\" | "^" | "[" | "]" | "`";
|
2008-08-09 15:20:12 +00:00
|
|
|
|
2009-01-13 12:48:03 +00:00
|
|
|
pchar = unreserved | pct_encoded | sub_delims | ":" | "@" | delims | unwise;
|
2008-08-09 15:20:12 +00:00
|
|
|
path = ("/" ( "/" | pchar)*) >mark %save_path;
|
|
|
|
|
|
|
|
# scheme = alpha *( alpha | digit | "+" | "-" | "." );
|
|
|
|
scheme = "http" | "https";
|
|
|
|
|
|
|
|
#simple ipv4 address
|
|
|
|
dec_octet = digit{1,3};
|
|
|
|
IPv4address = dec_octet "." dec_octet "." dec_octet "." dec_octet;
|
|
|
|
|
|
|
|
IPvFuture = "v" xdigit+ "." ( unreserved | sub_delims | ":" )+;
|
|
|
|
|
|
|
|
# simple ipv6 address
|
|
|
|
IPv6address = (":" | xdigit)+ IPv4address?;
|
|
|
|
|
|
|
|
IP_literal = "[" ( IPv6address | IPvFuture ) "]";
|
|
|
|
|
|
|
|
reg_name = ( unreserved | pct_encoded | sub_delims )+;
|
|
|
|
|
|
|
|
userinfo = ( unreserved | pct_encoded | sub_delims | ":" )*;
|
|
|
|
host = IP_literal | IPv4address | reg_name;
|
|
|
|
port = digit+;
|
|
|
|
authority = ( userinfo "@" )? (host >mark_host %save_host) ( ":" port )?;
|
|
|
|
|
|
|
|
query = ( pchar | "/" | "?" )* >mark %save_query;
|
|
|
|
fragment = ( pchar | "/" | "?" )*;
|
|
|
|
|
2009-10-14 17:29:08 +00:00
|
|
|
URI_path = (path ( "?" query )?) ( "#" fragment )?;
|
2008-08-09 15:20:12 +00:00
|
|
|
|
2009-09-13 15:45:41 +00:00
|
|
|
URI = (scheme >mark %save_scheme) "://" (authority >mark %save_authority) URI_path;
|
2008-08-09 15:20:12 +00:00
|
|
|
|
|
|
|
parse_URI := URI | ("*" >mark %save_path) | URI_path;
|
2008-08-10 19:31:56 +00:00
|
|
|
parse_Hostname := (host >mark_host %save_host) ( ":" port )?;
|
2008-08-09 15:20:12 +00:00
|
|
|
|
|
|
|
write data;
|
|
|
|
}%%
|
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
gboolean li_parse_raw_url(liRequestUri *uri) {
|
2008-08-09 15:20:12 +00:00
|
|
|
const char *p, *pe, *eof;
|
2009-10-14 17:34:44 +00:00
|
|
|
const char *mark = NULL, *host_mark = NULL;
|
2008-08-09 15:20:12 +00:00
|
|
|
int cs;
|
|
|
|
|
|
|
|
p = uri->raw->str;
|
|
|
|
eof = pe = uri->raw->str + uri->raw->len;
|
|
|
|
|
|
|
|
%% write init nocs;
|
|
|
|
cs = url_parser_en_parse_URI;
|
|
|
|
|
|
|
|
%% write exec;
|
|
|
|
|
|
|
|
return (cs >= url_parser_first_final);
|
|
|
|
}
|
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
gboolean li_parse_hostname(liRequestUri *uri) {
|
2008-08-09 15:20:12 +00:00
|
|
|
const char *p, *pe, *eof;
|
2009-10-14 17:34:44 +00:00
|
|
|
const char *mark = NULL, *host_mark = NULL;
|
2008-08-09 15:20:12 +00:00
|
|
|
int cs;
|
|
|
|
|
|
|
|
g_string_ascii_down(uri->authority);
|
|
|
|
p = uri->authority->str;
|
|
|
|
eof = pe = uri->authority->str + uri->authority->len;
|
|
|
|
|
|
|
|
%% write init nocs;
|
2008-08-10 19:31:56 +00:00
|
|
|
cs = url_parser_en_parse_Hostname;
|
2008-08-09 15:20:12 +00:00
|
|
|
|
|
|
|
%% write exec;
|
|
|
|
|
|
|
|
return (cs >= url_parser_first_final);
|
|
|
|
}
|