コード例 #1
0
 void request_context::finalise_header(http_parser* parser)
 {
     _current_hdr_value = nullptr;
     _current_hdr_name = nullptr;
     _current_header_object = nullptr;
     
     http_parser_url url_parser;
     http_parser_url_init(std::addressof(url_parser));
     auto result = http_parser_parse_url(mutable_request_header().uri().data(),
                                         mutable_request_header().uri().size(),
                                         parser->method == HTTP_CONNECT,
                                         std::addressof(url_parser));
     if (result) {
         BOOST_LOG_TRIVIAL(info) << "request_context::finalise_header - invalid url\n" << api::as_json(request_header());
         throw invalid_url(request_header().uri());
     }
     
     check_url_field(url_parser, mutable_request_header(), &HttpRequestHeader::QueryParts::mutable_schema, UF_SCHEMA);
     check_url_field(url_parser, mutable_request_header(), &HttpRequestHeader::QueryParts::mutable_host, UF_HOST);
     check_url_field(url_parser, mutable_request_header(), &HttpRequestHeader::QueryParts::mutable_port, UF_PORT);
     check_url_field(url_parser, mutable_request_header(), &HttpRequestHeader::QueryParts::mutable_path, UF_PATH);
     check_url_field(url_parser, mutable_request_header(), &HttpRequestHeader::QueryParts::mutable_query, UF_QUERY);
     check_url_field(url_parser, mutable_request_header(), &HttpRequestHeader::QueryParts::mutable_fragment, UF_FRAGMENT);
     check_url_field(url_parser, mutable_request_header(), &HttpRequestHeader::QueryParts::mutable_user_info, UF_USERINFO);
     mutable_request_header().set_method(http_method_str(static_cast<http_method>(parser->method)));
     mutable_request_header().set_version_major(parser->http_major);
     mutable_request_header().set_version_minor(parser->http_minor);
     _response_header->set_version_major(parser->http_major);
     _response_header->set_version_minor(parser->http_minor);
     BOOST_LOG_TRIVIAL(info) << "request_context::finalise_header - header complete:\n" << api::as_json(request_header());
 }
コード例 #2
0
ファイル: helpers.cpp プロジェクト: eggpi/xmms2-guilherme
	/** Decode an URL-encoded string.
	 *
	 * Some strings (currently only the url of media) have no known
	 * encoding, and must be encoded in an UTF-8 clean way. This is done
	 * similarly to the url encoding web browsers do. This functions decodes
	 * a string encoded in that way. OBSERVE that the decoded string HAS
	 * NO KNOWN ENCODING and you cannot display it on screen in a 100%
	 * guaranteed correct way (a good heuristic is to try to validate the
	 * decoded string as UTF-8, and if it validates assume that it is an
	 * UTF-8 encoded string, and otherwise fall back to some other
	 * encoding).
	 *
	 * Do not use this function if you don't understand the
	 * implications. The best thing is not to try to display the url at
	 * all.
	 *
	 * Note that the fact that the string has NO KNOWN ENCODING and CAN
	 * NOT BE DISPLAYED does not stop you from open the file if it is a
	 * local file (if it starts with "file://").
	 *
	 * @param encoded_url the url-encoded string
	 * @return the decoded string in an unknown encoding
	 *
	 */
	std::string decodeUrl( const std::string& encoded_url )
	{
		xmmsv_t *encoded, *decoded;
		const unsigned char *url;
		unsigned int len;
		std::string dec_str;

		encoded = xmmsv_new_string( encoded_url.c_str() );
		decoded = xmmsv_decode_url( encoded );
		if( !xmmsv_get_bin( decoded, &url, &len ) ) {
			throw invalid_url( "The given URL cannot be decoded." );
		}

		dec_str = std::string( reinterpret_cast< const char* >( url ), len );

		xmmsv_unref( encoded );
		xmmsv_unref( decoded );

		return dec_str;
	}