srs_core_http.cpp 11.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
/*
The MIT License (MIT)

Copyright (c) 2013 winlin

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.
*/

#include <srs_core_http.hpp>

#ifdef SRS_HTTP

#include <sstream>

#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <srs_core_error.hpp>
#include <srs_core_rtmp.hpp>
#include <srs_core_log.hpp>
#include <srs_core_socket.hpp>

#define SRS_DEFAULT_HTTP_PORT 80
#define SRS_HTTP_RESPONSE_OK "0"

SrsHttpUri::SrsHttpUri()
{
	port = SRS_DEFAULT_HTTP_PORT;
}

SrsHttpUri::~SrsHttpUri()
{
}

int SrsHttpUri::initialize(std::string _url)
{
	int ret = ERROR_SUCCESS;
    
    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;
    }
    
	std::string field = get_uri_field(url, &hp_u, UF_SCHEMA);
    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);
    
	return ret;
}

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;
}

const char* SrsHttpUri::get_path()
{
    return path.c_str();
}

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);
}

SrsHttpClient::SrsHttpClient()
{
	connected = false;
	stfd = NULL;
}

SrsHttpClient::~SrsHttpClient()
{
	disconnect();
}

int SrsHttpClient::post(SrsHttpUri* uri, std::string req, std::string& res)
{
	int ret = ERROR_SUCCESS;
	
	if ((ret = connect(uri)) != ERROR_SUCCESS) {
		srs_error("http connect server failed. ret=%d", ret);
		return ret;
	}
	
    // send POST request to uri
    // POST %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n\r\n%s
    std::stringstream ss;
    ss << "POST " << uri->get_path() << " "
        << "HTTP/1.1\r\n"
        << "Host: " << uri->get_host() << "\r\n"
        << "Connection: Keep-Alive" << "\r\n"
        << "Content-Length: " << std::dec << req.length() << "\r\n"
        << "User-Agent: " << RTMP_SIG_SRS_NAME << RTMP_SIG_SRS_VERSION << "\r\n"
        << "Content-Type: text/html" << "\r\n"
        << "\r\n"
        << req;
    
    SrsSocket skt(stfd);
    
    std::string data = ss.str();
    ssize_t nwrite;
    if ((ret = skt.write(data.c_str(), data.length(), &nwrite)) != ERROR_SUCCESS) {
        // disconnect when error.
        disconnect();
        
        srs_error("write http post failed. ret=%d", ret);
        return ret;
    }
    
    if ((ret = parse_response(uri, &skt, &res)) != ERROR_SUCCESS) {
        srs_error("parse http post response failed. ret=%d", ret);
        return ret;
    }
    srs_info("parse http post response success.");
	
	return ret;
}

void SrsHttpClient::disconnect()
{
	connected = false;
	
	if (stfd) {
		int fd = st_netfd_fileno(stfd);
		st_netfd_close(stfd);
		stfd = NULL;
		
		// st does not close it sometimes, 
		// close it manually.
		::close(fd);
	}
}

int SrsHttpClient::connect(SrsHttpUri* uri)
{
	int ret = ERROR_SUCCESS;
	
	if (connected) {
		return ret;
	}
	
	disconnect();
	
	std::string ip = srs_dns_resolve(uri->get_host());
	if (ip.empty()) {
		ret = ERROR_SYSTEM_IP_INVALID;
		srs_error("dns resolve server error, ip empty. ret=%d", ret);
		return ret;
	}

    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock == -1){
        ret = ERROR_SOCKET_CREATE;
        srs_error("create socket error. ret=%d", ret);
        return ret;
    }
    
    stfd = st_netfd_open_socket(sock);
    if(stfd == NULL){
        ret = ERROR_ST_OPEN_SOCKET;
        srs_error("st_netfd_open_socket failed. ret=%d", ret);
        return ret;
    }
	
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(uri->get_port());
    addr.sin_addr.s_addr = inet_addr(ip.c_str());
    
    if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(sockaddr_in), ST_UTIME_NO_TIMEOUT) == -1){
        ret = ERROR_ST_CONNECT;
        srs_error("connect to server error. "
        	"ip=%s, port=%d, ret=%d", ip.c_str(), uri->get_port(), ret);
        return ret;
    }
    srs_info("connect to server success. "
    	"http url=%s, server=%s, ip=%s, port=%d", 
    	uri->get_url(), uri->get_host(), ip.c_str(), uri->get_port());
    
    connected = true;
    
	return ret;
}

int SrsHttpClient::parse_response(SrsHttpUri* uri, SrsSocket* skt, std::string* response)
{
    int ret = ERROR_SUCCESS;

    int body_received = 0;
    if ((ret = parse_response_header(skt, response, body_received)) != ERROR_SUCCESS) {
        srs_error("parse response header failed. ret=%d", ret);
        return ret;
    }

    if ((ret = parse_response_body(uri, skt, response, body_received)) != ERROR_SUCCESS) {
        srs_error("parse response body failed. ret=%d", ret);
        return ret;
    }

    srs_info("url %s download, body size=%"PRId64, uri->get_url(), http_header.content_length);
    
    return ret;
}

int SrsHttpClient::parse_response_header(SrsSocket* skt, std::string* response, int& body_received)
{
    int ret = ERROR_SUCCESS;

    http_parser_settings settings;
    
    memset(&settings, 0, sizeof(settings));
    settings.on_headers_complete = on_headers_complete;
    
    http_parser parser;
    http_parser_init(&parser, HTTP_RESPONSE);
    // callback object ptr.
    parser.data = (void*)this;
    
    // reset response header.
    memset(&http_header, 0, sizeof(http_header));
    
    // parser header.
    char buf[SRS_HTTP_HEADER_BUFFER];
    for (;;) {
        ssize_t nread;
        if ((ret = skt->read(buf, (size_t)sizeof(buf), &nread)) != ERROR_SUCCESS) {
            srs_error("read body from server failed. ret=%d", ret);
            return ret;
        }
        
        ssize_t nparsed = http_parser_execute(&parser, &settings, buf, nread);
        srs_info("read_size=%d, nparsed=%d", (int)nread, (int)nparsed);

        // check header size.
        if (http_header.nread != 0) {
            body_received = nread - nparsed;
            
            srs_info("http header parsed, size=%d, content-length=%"PRId64", body-received=%d", 
                http_header.nread, http_header.content_length, body_received);
                
            if(response != NULL && body_received > 0){
                response->append(buf + nparsed, body_received);
            }

            return ret;
        }
        
        if (nparsed != nread) {
            ret = ERROR_HTTP_PARSE_HEADER;
            srs_error("parse response error, parsed(%d)!=read(%d), ret=%d", (int)nparsed, (int)nread, ret);
            return ret;
        }
    }
    
    return ret;
}

int SrsHttpClient::parse_response_body(SrsHttpUri* uri, SrsSocket* skt, std::string* response, int body_received)
{
    int ret = ERROR_SUCCESS;
    
    srs_assert(uri != NULL);
    
    uint64_t body_left = http_header.content_length - body_received;
    
    if (body_left <= 0) {
        return ret;
    }
    
    if (response != NULL) {
        char buf[SRS_HTTP_BODY_BUFFER];
        
        return parse_response_body_data(
        	uri, skt, response, (size_t)body_left, 
        	(const void*)buf, (size_t)SRS_HTTP_BODY_BUFFER
        );
    } else {
        // if ignore response, use shared fast memory.
        static char buf[SRS_HTTP_BODY_BUFFER];
        
        return parse_response_body_data(
			uri, skt, response, (size_t)body_left, 
			(const void*)buf, (size_t)SRS_HTTP_BODY_BUFFER
		);
    }
    
    return ret;
}

int SrsHttpClient::parse_response_body_data(SrsHttpUri* uri, SrsSocket* skt, std::string* response, size_t body_left, const void* buf, size_t size)
{
    int ret = ERROR_SUCCESS;
    
    srs_assert(uri != NULL);
    
    while (body_left > 0) {
        ssize_t nread;
		int size_to_read = srs_min(size, body_left);
        if ((ret = skt->read(buf, size_to_read, &nread)) != ERROR_SUCCESS) {
            srs_error("read header from server failed. ret=%d", ret);
            return ret;
        }
        
        if (response != NULL && nread > 0) {
            response->append((char*)buf, nread);
        }
        
        body_left -= nread;
        srs_info("read url(%s) content partial %"PRId64"/%"PRId64"", 
            uri->get_url(), http_header.content_length - body_left, http_header.content_length);
    }
    
    return ret;
}

int SrsHttpClient::on_headers_complete(http_parser* parser)
{
    SrsHttpClient* obj = (SrsHttpClient*)parser->data;
    obj->comple_header(parser);
    
    // see http_parser.c:1570, return 1 to skip body.
    return 1;
}

void SrsHttpClient::comple_header(http_parser* parser)
{
    // save the parser status when header parse completed.
    memcpy(&http_header, parser, sizeof(http_header));
}

SrsHttpHooks::SrsHttpHooks()
{
}

SrsHttpHooks::~SrsHttpHooks()
{
}

int SrsHttpHooks::on_connect(std::string url, std::string ip, SrsRequest* req)
{
	int ret = ERROR_SUCCESS;
	
	SrsHttpUri uri;
	if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {
		srs_error("http uri parse url failed. "
			"url=%s, ret=%d", url.c_str(), ret);
		return ret;
	}
	
	/**
	{
		"ip": "192.168.1.10", "vhost": "video.test.com", "app": "live",
		"pageUrl": "http://www.test.com/live.html"
	}
    */
	std::stringstream ss;
	ss << "{"
		// ip
		<< '"' << "ip" << '"' << ':'
		<< '"' << ip << '"'
		<< ','
		// vhost
		<< '"' << "vhost" << '"' << ':'
		<< '"' << req->vhost << '"'
		<< ','
		// app
		<< '"' << "app" << '"' << ':'
		<< '"' << req->app << '"'
		<< ','
		// pageUrl
		<< '"' << "pageUrl" << '"' << ':'
		<< '"' << req->pageUrl << '"'
		//<< ','
		<< "}";
	std::string data = ss.str();
	std::string res;
	
	SrsHttpClient http;
	if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) {
		srs_error("http post uri failed. "
			"url=%s, request=%s, response=%s, ret=%d",
			url.c_str(), data.c_str(), res.c_str(), ret);
		return ret;
	}
	
	if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {
		ret = ERROR_HTTP_DATA_INVLIAD;
		srs_error("http hook validate failed. "
			"res=%s, ret=%d", res.c_str(), ret);
		return ret;
	}
	
	srs_trace("http hook on_connect success. "
		"url=%s, request=%s, response=%s, ret=%d",
		url.c_str(), data.c_str(), res.c_str(), ret);
	
	return ret;
}

#endif