2013-12-07 12:51:27 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2014-01-01 02:37:12 +00:00
|
|
|
Copyright (c) 2013-2014 winlin
|
2013-12-07 12:51:27 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-02 13:49:09 +00:00
|
|
|
#include <srs_app_http.hpp>
|
2013-12-07 12:51:27 +00:00
|
|
|
|
2014-04-01 10:40:24 +00:00
|
|
|
#ifdef SRS_HTTP_PARSER
|
2013-12-07 15:23:19 +00:00
|
|
|
|
2013-12-07 14:06:53 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-03-01 02:30:16 +00:00
|
|
|
#include <srs_kernel_error.hpp>
|
2014-03-01 02:42:55 +00:00
|
|
|
#include <srs_kernel_log.hpp>
|
2014-03-02 13:49:09 +00:00
|
|
|
#include <srs_app_socket.hpp>
|
2013-12-07 14:06:53 +00:00
|
|
|
|
|
|
|
#define SRS_DEFAULT_HTTP_PORT 80
|
|
|
|
|
2014-04-01 10:40:24 +00:00
|
|
|
SrsHttpMessage::SrsHttpMessage()
|
|
|
|
{
|
|
|
|
body = new SrsBuffer();
|
|
|
|
state = SrsHttpParseStateInit;
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsHttpMessage::~SrsHttpMessage()
|
|
|
|
{
|
|
|
|
srs_freep(body);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrsHttpMessage::reset()
|
|
|
|
{
|
|
|
|
state = SrsHttpParseStateInit;
|
|
|
|
body->clear();
|
|
|
|
url = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SrsHttpMessage::is_complete()
|
|
|
|
{
|
|
|
|
return state == SrsHttpParseStateComplete;
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsHttpParser::SrsHttpParser()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsHttpParser::~SrsHttpParser()
|
|
|
|
{
|
|
|
|
}
|
2014-04-01 04:36:56 +00:00
|
|
|
|
2013-12-07 14:06:53 +00:00
|
|
|
SrsHttpUri::SrsHttpUri()
|
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
port = SRS_DEFAULT_HTTP_PORT;
|
2013-12-07 14:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsHttpUri::~SrsHttpUri()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsHttpUri::initialize(std::string _url)
|
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
int ret = ERROR_SUCCESS;
|
2013-12-07 14:06:53 +00:00
|
|
|
|
|
|
|
url = _url;
|
|
|
|
const char* purl = url.c_str();
|
|
|
|
|
|
|
|
http_parser_url hp_u;
|
|
|
|
if((ret = http_parser_parse_url(purl, url.length(), 0, &hp_u)) != 0){
|
|
|
|
int code = ret;
|
|
|
|
ret = ERROR_HTTP_PARSE_URI;
|
|
|
|
|
|
|
|
srs_error("parse url %s failed, code=%d, ret=%d", purl, code, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-03-18 03:32:58 +00:00
|
|
|
std::string field = get_uri_field(url, &hp_u, UF_SCHEMA);
|
2013-12-07 14:06:53 +00:00
|
|
|
if(!field.empty()){
|
|
|
|
schema = field;
|
|
|
|
}
|
|
|
|
|
|
|
|
host = get_uri_field(url, &hp_u, UF_HOST);
|
|
|
|
|
|
|
|
field = get_uri_field(url, &hp_u, UF_PORT);
|
|
|
|
if(!field.empty()){
|
|
|
|
port = atoi(field.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
path = get_uri_field(url, &hp_u, UF_PATH);
|
|
|
|
srs_info("parse url %s success", purl);
|
|
|
|
|
2014-03-18 03:32:58 +00:00
|
|
|
return ret;
|
2013-12-07 14:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* SrsHttpUri::get_url()
|
|
|
|
{
|
|
|
|
return url.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* SrsHttpUri::get_schema()
|
|
|
|
{
|
|
|
|
return schema.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* SrsHttpUri::get_host()
|
|
|
|
{
|
|
|
|
return host.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsHttpUri::get_port()
|
|
|
|
{
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
2013-12-07 15:23:19 +00:00
|
|
|
const char* SrsHttpUri::get_path()
|
|
|
|
{
|
|
|
|
return path.c_str();
|
|
|
|
}
|
|
|
|
|
2013-12-07 14:06:53 +00:00
|
|
|
std::string SrsHttpUri::get_uri_field(std::string uri, http_parser_url* hp_u, http_parser_url_fields field)
|
|
|
|
{
|
|
|
|
if((hp_u->field_set & (1 << field)) == 0){
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_verbose("uri field matched, off=%d, len=%d, value=%.*s",
|
|
|
|
hp_u->field_data[field].off,
|
|
|
|
hp_u->field_data[field].len,
|
|
|
|
hp_u->field_data[field].len,
|
|
|
|
uri.c_str() + hp_u->field_data[field].off);
|
|
|
|
|
|
|
|
int offset = hp_u->field_data[field].off;
|
|
|
|
int len = hp_u->field_data[field].len;
|
|
|
|
|
|
|
|
return uri.substr(offset, len);
|
|
|
|
}
|
|
|
|
|
2013-12-07 12:51:27 +00:00
|
|
|
#endif
|