std::string get_scheme(TextCursor cursor) { int scheme_length; TextCursor scheme_cursor(parse_scheme(cursor)); scheme_length = scheme_cursor.get_offset() - cursor.get_offset(); char scheme[scheme_length + 1]; cursor.gets(scheme_length, scheme); return scheme; }
/************************************************************************ * Function : parse_uri * * Parameters : * char * in ; character string containing uri information to be * parsed * int max ; maximum limit on the number of characters * uri_type * out ; out parameter which will have the parsed uri * information * * Description : parses a uri as defined in http://www.ietf.org/rfc/ * rfc2396.txt (RFC explaining URIs) * Handles absolute, relative, and opaque uris. Parses into the * following pieces: scheme, hostport, pathquery, fragment (path and * query are treated as one token) * Caller should check for the pieces they require. * * Return : int ; * * Note : ************************************************************************/ int parse_uri( const char *in, int max, uri_type * out ) { int begin_path = 0; int begin_hostport = 0; int begin_fragment = 0; if( ( begin_hostport = parse_scheme( in, max, &out->scheme ) ) ) { out->type = ABSOLUTE; out->path_type = OPAQUE_PART; begin_hostport++; } else { out->type = RELATIVE; out->path_type = REL_PATH; } if( ( ( begin_hostport + 1 ) < max ) && ( in[begin_hostport] == '/' ) && ( in[begin_hostport + 1] == '/' ) ) { begin_hostport += 2; if( ( begin_path = parse_hostport( &in[begin_hostport], max - begin_hostport, &out->hostport ) ) >= 0 ) { begin_path += begin_hostport; } else return begin_path; } else { out->hostport.IPv4address.sin_port = 0; out->hostport.IPv4address.sin_addr.s_addr = 0; out->hostport.text.size = 0; out->hostport.text.buff = 0; begin_path = begin_hostport; } begin_fragment = parse_uric( &in[begin_path], max - begin_path, &out->pathquery ) + begin_path; if( ( out->pathquery.size ) && ( out->pathquery.buff[0] == '/' ) ) { out->path_type = ABS_PATH; } if( ( begin_fragment < max ) && ( in[begin_fragment] == '#' ) ) { begin_fragment++; parse_uric( &in[begin_fragment], max - begin_fragment, &out->fragment ); } else { out->fragment.buff = NULL; out->fragment.size = 0; } return HTTP_SUCCESS; }
int parse_uri(const char *in, size_t max, uri_type *out) { int begin_path = 0; size_t begin_hostport = (size_t)0; size_t begin_fragment = (size_t)0; unsigned short int defaultPort = 80; begin_hostport = parse_scheme(in, max, &out->scheme); if (begin_hostport) { out->type = ABSOLUTE; out->path_type = OPAQUE_PART; begin_hostport++; } else { out->type = RELATIVE; out->path_type = REL_PATH; } if (begin_hostport + (size_t)1 < max && in[begin_hostport] == '/' && in[begin_hostport + (size_t)1] == '/') { begin_hostport += (size_t)2; if (token_string_casecmp(&out->scheme, "https") == 0) { defaultPort = 443; } begin_path = parse_hostport(&in[begin_hostport], defaultPort, &out->hostport); if (begin_path >= 0) { begin_path += (int)begin_hostport; } else return begin_path; } else { memset(&out->hostport, 0, sizeof(out->hostport)); begin_path = (int)begin_hostport; } begin_fragment = parse_uric(&in[begin_path], max - (size_t)begin_path, &out->pathquery) + (size_t)begin_path; if (out->pathquery.size && out->pathquery.buff[0] == '/') { out->path_type = ABS_PATH; } if (begin_fragment < max && in[begin_fragment] == '#') { begin_fragment++; parse_uric(&in[begin_fragment], max - begin_fragment, &out->fragment); } else { out->fragment.buff = NULL; out->fragment.size = (size_t)0; } return HTTP_SUCCESS; }
parsed_url* parse_url( const char* url, void* usermem, size_t mem_size ) { void* mem = usermem; if( mem == 0x0 ) { mem_size = parse_url_calc_mem_usage( url ); mem = malloc( mem_size ); } parse_url_ctx ctx = { mem, mem_size, mem_size }; parsed_url* out = (parsed_url*)alloc_mem( &ctx, sizeof( parsed_url ) ); URL_PARSE_FAIL_IF( out == 0x0 ); url = parse_scheme( url, &ctx, out ); URL_PARSE_FAIL_IF( url == 0x0 ); url = parse_user_pass( url, &ctx, out ); URL_PARSE_FAIL_IF( url == 0x0 ); url = parse_host_port( url, &ctx, out ); URL_PARSE_FAIL_IF( url == 0x0 ); return out; }
uri_t* parse_uri(char* uri_string, int len) { uri_t* uri = NULL; char* tmp_uri = NULL; char* uri_pointer = NULL; if(!(tmp_uri = (char*)calloc((strlen(uri_string) + 1), sizeof(char)))) { printf("Could not alloc memory for tmp (uri string)!\n"); return NULL; } memcpy(tmp_uri, uri_string, strlen(uri_string)); if(!(uri = (uri_t*)calloc(1, sizeof(uri_t)))) { printf("Could not alloc memory for uri structure!\n"); free(tmp_uri); return NULL; } uri_pointer = parse_scheme(uri, tmp_uri); if(strcmp(uri_pointer, uri_string) == 0) { uri_pointer = parse_path(uri, uri_pointer); if(uri_pointer != NULL) { uri_pointer = parse_frag(uri, uri_pointer); } } else { uri_pointer = parse_authority(uri, uri_pointer); uri_pointer = parse_path(uri, uri_pointer); if(uri_pointer != NULL) { uri_pointer = parse_frag(uri, uri_pointer); } } free(tmp_uri); return uri; }
// URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] // ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? // 12 3 4 5 6 7 8 9 static IteratorT parse(IteratorT begin, IteratorT end, Components & components) { // (([^:/?#]+):)? IteratorT scheme_begin = begin; IteratorT scheme_end = parse_scheme(scheme_begin, end); if (scheme_end != scheme_begin) { components.scheme_begin = scheme_begin; components.scheme_end = scheme_end - 1; } IteratorT hierarchy_begin = parse_constant(":", scheme_end, end); IteratorT hierarchy_end = parse_hierarchy(hierarchy_begin, end, components.hierarchy); if (hierarchy_end == hierarchy_begin) { return hierarchy_begin; } components.hierarchy_begin = hierarchy_begin; components.hierarchy_end = hierarchy_end; IteratorT query_begin = hierarchy_end; IteratorT query_end = parse_meta("?", query_begin, end); if (query_end != query_begin) { components.query_begin = query_begin; components.query_end = query_end; } IteratorT fragment_begin = query_end; IteratorT fragment_end = parse_meta("#", fragment_begin, end); if (fragment_end != fragment_begin) { components.fragment_begin = fragment_begin; components.fragment_end = fragment_end; } components.complete = true; return fragment_end; }
//uri = scheme ":" hier-part [ "?" query ] [ "#" fragment ] TextCursor parse_uri(TextCursor cursor) { cursor = parse_scheme(cursor); if(get_char(cursor) != ':') throw ParseError(); cursor = parse_hier_part(cursor); try { cursor = parse_query(cursor); } catch(ParseError) { } try { cursor = parse_fragment(cursor); } catch(ParseError) { } return cursor; }