Blame view

trunk/src/app/srs_app_http.cpp 21.0 KB
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2014 winlin
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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.
*/
24
#include <srs_app_http.hpp>
25
26
#ifdef SRS_HTTP_PARSER
27
winlin authored
28 29
#include <stdlib.h>
30 31
using namespace std;
32
#include <srs_kernel_error.hpp>
33
#include <srs_kernel_log.hpp>
34
#include <srs_app_socket.hpp>
winlin authored
35 36
#include <srs_app_http_api.hpp>
#include <srs_app_http_conn.hpp>
37
#include <srs_app_json.hpp>
winlin authored
38 39 40

#define SRS_DEFAULT_HTTP_PORT 80
winlin authored
41 42
#define SRS_HTTP_HEADER_BUFFER 1024
43 44 45 46 47 48 49 50
bool srs_path_equals(const char* expect, const char* path, int nb_path)
{
    int size = strlen(expect);
    
    if (size != nb_path) {
        return false;
    }
    
51 52
    bool equals = !memcmp(expect, path, size);
    return equals;
53 54
}
55 56 57 58 59 60 61
bool srs_path_like(const char* expect, const char* path, int nb_path)
{
    int size = strlen(expect);
    bool equals = !strncmp(expect, path, srs_min(size, nb_path));
    return equals;
}
62 63 64 65 66
SrsHttpHandlerMatch::SrsHttpHandlerMatch()
{
    handler = NULL;
}
winlin authored
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
SrsHttpHandler::SrsHttpHandler()
{
}

SrsHttpHandler::~SrsHttpHandler()
{
    std::vector<SrsHttpHandler*>::iterator it;
    for (it = handlers.begin(); it != handlers.end(); ++it) {
        SrsHttpHandler* handler = *it;
        srs_freep(handler);
    }
    handlers.clear();
}

int SrsHttpHandler::initialize()
{
    int ret = ERROR_SUCCESS;
    return ret;
}
87
bool SrsHttpHandler::can_handle(const char* /*path*/, int /*length*/, const char** /*pchild*/)
winlin authored
88 89 90 91
{
    return false;
}
92 93 94
int SrsHttpHandler::process_request(SrsSocket* skt, SrsHttpMessage* req)
{
    if (req->method() == HTTP_OPTIONS) {
95
        req->set_requires_crossdomain(true);
96 97
        return res_options(skt);
    }
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

    int status_code;
    std::string reason_phrase;
    if (!is_handler_valid(req, status_code, reason_phrase)) {
        std::stringstream ss;
        
        ss << JOBJECT_START
            << JFIELD_ERROR(ERROR_HTTP_HANDLER_INVALID) << JFIELD_CONT
            << JFIELD_ORG("data", JOBJECT_START)
                << JFIELD_ORG("status_code", status_code) << JFIELD_CONT
                << JFIELD_STR("reason_phrase", reason_phrase) << JFIELD_CONT
                << JFIELD_STR("url", req->url())
            << JOBJECT_END
            << JOBJECT_END;
        
113
        return res_error(skt, req, status_code, reason_phrase, ss.str());
114
    }
115 116 117 118
    
    return do_process_request(skt, req);
}
119 120 121 122 123 124 125 126 127 128 129 130
bool SrsHttpHandler::is_handler_valid(SrsHttpMessage* req, int& status_code, std::string& reason_phrase) 
{
    if (!req->match()->unmatched_url.empty()) {
        status_code = HTTP_NotFound;
        reason_phrase = HTTP_NotFound_str;
        
        return false;
    }
    
    return true;
}
131
int SrsHttpHandler::do_process_request(SrsSocket* /*skt*/, SrsHttpMessage* /*req*/)
winlin authored
132 133 134 135 136
{
    int ret = ERROR_SUCCESS;
    return ret;
}
137
int SrsHttpHandler::best_match(const char* path, int length, SrsHttpHandlerMatch** ppmatch)
winlin authored
138 139 140
{
    int ret = ERROR_SUCCESS;
    
141 142 143 144
    SrsHttpHandler* handler = NULL;
    const char* match_start = NULL;
    int match_length = 0;
    
winlin authored
145 146 147 148 149 150 151 152 153 154 155 156
    for (;;) {
        // ensure cur is not NULL.
        // ensure p not NULL and has bytes to parse.
        if (!path || length <= 0) {
            break;
        }
        
        const char* p = NULL;
        for (p = path + 1; p - path < length && *p != __PATH_SEP; p++) {
        }
        
        // whether the handler can handler the node.
157 158
        const char* pchild = p;
        if (!can_handle(path, p - path, &pchild)) {
winlin authored
159 160 161
            break;
        }
        
162
        // save current handler, it's ok for current handler atleast.
163 164 165
        handler = this;
        match_start = path;
        match_length = p - path;
winlin authored
166
        
167
        // find the best matched child handler.
winlin authored
168 169
        std::vector<SrsHttpHandler*>::iterator it;
        for (it = handlers.begin(); it != handlers.end(); ++it) {
170
            SrsHttpHandler* h = *it;
171 172
            
            // matched, donot search more.
173
            if (h->best_match(pchild, length - (pchild - path), ppmatch) == ERROR_SUCCESS) {
winlin authored
174 175 176 177 178 179 180 181
                break;
            }
        }
        
        // whatever, donot loop.
        break;
    }
    
182 183 184 185 186 187 188
    // if already matched by child, return.
    if (*ppmatch) {
        return ret;
    }
    
    // not matched, error.
    if (handler == NULL) {
winlin authored
189 190 191 192
        ret = ERROR_HTTP_HANDLER_MATCH_URL;
        return ret;
    }
    
193 194 195 196 197
    // matched by this handler.
    *ppmatch = new SrsHttpHandlerMatch();
    (*ppmatch)->handler = handler;
    (*ppmatch)->matched_url.append(match_start, match_length);
    
198 199 200 201 202
    int unmatch_length = length - match_length;
    if (unmatch_length > 0) {
        (*ppmatch)->unmatched_url.append(match_start + match_length, unmatch_length);
    }
    
winlin authored
203 204 205
    return ret;
}
206 207 208 209 210 211 212
SrsHttpHandler* SrsHttpHandler::res_status_line(std::stringstream& ss)
{
    ss << "HTTP/1.1 200 OK " << __CRLF
       << "Server: SRS/"RTMP_SIG_SRS_VERSION"" << __CRLF;
    return this;
}
213 214 215 216 217 218 219
SrsHttpHandler* SrsHttpHandler::res_status_line_error(std::stringstream& ss, int code, std::string reason_phrase)
{
    ss << "HTTP/1.1 " << code << " " << reason_phrase << __CRLF
       << "Server: SRS/"RTMP_SIG_SRS_VERSION"" << __CRLF;
    return this;
}
220 221 222 223 224 225 226
SrsHttpHandler* SrsHttpHandler::res_content_type(std::stringstream& ss)
{
    ss << "Content-Type: text/html;charset=utf-8" << __CRLF
        << "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __CRLF;
    return this;
}
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
SrsHttpHandler* SrsHttpHandler::res_content_type_xml(std::stringstream& ss)
{
    ss << "Content-Type: text/xml;charset=utf-8" << __CRLF
        << "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __CRLF;
    return this;
}

SrsHttpHandler* SrsHttpHandler::res_content_type_javascript(std::stringstream& ss)
{
    ss << "Content-Type: text/javascript;charset=utf-8" << __CRLF
        << "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __CRLF;
    return this;
}

SrsHttpHandler* SrsHttpHandler::res_content_type_swf(std::stringstream& ss)
{
    ss << "Content-Type: application/x-shockwave-flash;charset=utf-8" << __CRLF
        << "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __CRLF;
    return this;
}

SrsHttpHandler* SrsHttpHandler::res_content_type_css(std::stringstream& ss)
{
    ss << "Content-Type: text/css;charset=utf-8" << __CRLF
        << "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __CRLF;
    return this;
}
255 256 257 258 259 260 261
SrsHttpHandler* SrsHttpHandler::res_content_type_json(std::stringstream& ss)
{
    ss << "Content-Type: application/json;charset=utf-8" << __CRLF
        << "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __CRLF;
    return this;
}
262 263 264 265 266 267 268 269 270 271 272 273 274 275
SrsHttpHandler* SrsHttpHandler::res_content_type_m3u8(std::stringstream& ss)
{
    ss << "Content-Type: application/x-mpegURL;charset=utf-8" << __CRLF
        << "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __CRLF;
    return this;
}

SrsHttpHandler* SrsHttpHandler::res_content_type_mpegts(std::stringstream& ss)
{
    ss << "Content-Type: video/MP2T;charset=utf-8" << __CRLF
        << "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __CRLF;
    return this;
}
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
SrsHttpHandler* SrsHttpHandler::res_content_length(std::stringstream& ss, int64_t length)
{
    ss << "Content-Length: "<< length << __CRLF;
    return this;
}

SrsHttpHandler* SrsHttpHandler::res_enable_crossdomain(std::stringstream& ss)
{
    ss << "Access-Control-Allow-Origin: *" << __CRLF
        << "Access-Control-Allow-Methods: "
        << "GET, POST, HEAD, PUT, DELETE" << __CRLF
        << "Access-Control-Allow-Headers: "
        << "Cache-Control,X-Proxy-Authorization,X-Requested-With,Content-Type" << __CRLF;
    return this;
}

SrsHttpHandler* SrsHttpHandler::res_header_eof(std::stringstream& ss)
{
    ss << __CRLF;
    return this;
}

SrsHttpHandler* SrsHttpHandler::res_body(std::stringstream& ss, std::string body)
{
    ss << body;
    return this;
}

int SrsHttpHandler::res_flush(SrsSocket* skt, std::stringstream& ss)
{
    return skt->write(ss.str().c_str(), ss.str().length(), NULL);
}

int SrsHttpHandler::res_options(SrsSocket* skt)
{
    std::stringstream ss;
    
    res_status_line(ss)->res_content_type(ss)
        ->res_content_length(ss, 0)->res_enable_crossdomain(ss)
        ->res_header_eof(ss);
    
    return res_flush(skt, ss);
}
320
int SrsHttpHandler::res_text(SrsSocket* skt, SrsHttpMessage* req, std::string body)
321 322 323 324
{
    std::stringstream ss;
    
    res_status_line(ss)->res_content_type(ss)
325 326 327 328 329 330 331
        ->res_content_length(ss, (int)body.length());
        
    if (req->requires_crossdomain()) {
        res_enable_crossdomain(ss);
    }
    
    res_header_eof(ss)
332 333 334 335 336
        ->res_body(ss, body);
    
    return res_flush(skt, ss);
}
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
int SrsHttpHandler::res_xml(SrsSocket* skt, SrsHttpMessage* req, std::string body)
{
    std::stringstream ss;
    
    res_status_line(ss)->res_content_type_xml(ss)
        ->res_content_length(ss, (int)body.length());
        
    if (req->requires_crossdomain()) {
        res_enable_crossdomain(ss);
    }
    
    res_header_eof(ss)
        ->res_body(ss, body);
    
    return res_flush(skt, ss);
}

int SrsHttpHandler::res_javascript(SrsSocket* skt, SrsHttpMessage* req, std::string body)
{
    std::stringstream ss;
    
    res_status_line(ss)->res_content_type_javascript(ss)
        ->res_content_length(ss, (int)body.length());
        
    if (req->requires_crossdomain()) {
        res_enable_crossdomain(ss);
    }
    
    res_header_eof(ss)
        ->res_body(ss, body);
    
    return res_flush(skt, ss);
}

int SrsHttpHandler::res_swf(SrsSocket* skt, SrsHttpMessage* req, std::string body)
{
    std::stringstream ss;
    
    res_status_line(ss)->res_content_type_swf(ss)
        ->res_content_length(ss, (int)body.length());
        
    if (req->requires_crossdomain()) {
        res_enable_crossdomain(ss);
    }
    
    res_header_eof(ss)
        ->res_body(ss, body);
    
    return res_flush(skt, ss);
}

int SrsHttpHandler::res_css(SrsSocket* skt, SrsHttpMessage* req, std::string body)
{
    std::stringstream ss;
    
    res_status_line(ss)->res_content_type_css(ss)
        ->res_content_length(ss, (int)body.length());
        
    if (req->requires_crossdomain()) {
        res_enable_crossdomain(ss);
    }
    
    res_header_eof(ss)
        ->res_body(ss, body);
    
    return res_flush(skt, ss);
}
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
int SrsHttpHandler::res_m3u8(SrsSocket* skt, SrsHttpMessage* req, std::string body)
{
    std::stringstream ss;
    
    res_status_line(ss)->res_content_type_m3u8(ss)
        ->res_content_length(ss, (int)body.length());
        
    if (req->requires_crossdomain()) {
        res_enable_crossdomain(ss);
    }
    
    res_header_eof(ss)
        ->res_body(ss, body);
    
    return res_flush(skt, ss);
}

int SrsHttpHandler::res_mpegts(SrsSocket* skt, SrsHttpMessage* req, std::string body)
{
    std::stringstream ss;
    
    res_status_line(ss)->res_content_type_mpegts(ss)
        ->res_content_length(ss, (int)body.length());
        
    if (req->requires_crossdomain()) {
        res_enable_crossdomain(ss);
    }
    
    res_header_eof(ss)
        ->res_body(ss, body);
    
    return res_flush(skt, ss);
}
439
int SrsHttpHandler::res_json(SrsSocket* skt, SrsHttpMessage* req, std::string json)
440 441 442 443
{
    std::stringstream ss;
    
    res_status_line(ss)->res_content_type_json(ss)
444 445 446 447 448 449 450
        ->res_content_length(ss, (int)json.length());
        
    if (req->requires_crossdomain()) {
        res_enable_crossdomain(ss);
    }
    
    res_header_eof(ss)
451 452 453 454 455
        ->res_body(ss, json);
    
    return res_flush(skt, ss);
}
456
int SrsHttpHandler::res_error(SrsSocket* skt, SrsHttpMessage* req, int code, std::string reason_phrase, std::string body)
457 458 459
{
    std::stringstream ss;
460
    res_status_line_error(ss, code, reason_phrase)->res_content_type_json(ss)
461 462 463 464 465 466 467
        ->res_content_length(ss, (int)body.length());
        
    if (req->requires_crossdomain()) {
        res_enable_crossdomain(ss);
    }
    
    res_header_eof(ss)
468 469 470 471 472
        ->res_body(ss, body);
    
    return res_flush(skt, ss);
}
winlin authored
473 474 475 476 477 478 479
SrsHttpHandler* SrsHttpHandler::create_http_api()
{
    return new SrsApiRoot();
}

SrsHttpHandler* SrsHttpHandler::create_http_stream()
{
winlin authored
480
    return new SrsHttpRoot();
winlin authored
481
}
482
483 484
SrsHttpMessage::SrsHttpMessage()
{
485 486
    _body = new SrsBuffer();
    _state = SrsHttpParseStateInit;
487 488
    _uri = new SrsHttpUri();
    _match = NULL;
489
    _requires_crossdomain = false;
490 491 492 493
}

SrsHttpMessage::~SrsHttpMessage()
{
494
    srs_freep(_body);
495 496
    srs_freep(_uri);
    srs_freep(_match);
497 498 499 500
}

void SrsHttpMessage::reset()
{
501 502 503
    _state = SrsHttpParseStateInit;
    _body->clear();
    _url = "";
504 505
}
506 507
int SrsHttpMessage::parse_uri()
{
508 509 510 511 512 513 514 515 516 517 518 519 520 521
    // filter url according to HTTP specification.
    
    // remove the duplicated slash.
    std::string filtered_url = srs_string_replace(_url, "//", "/");
    
    // remove the last / to match resource.
    filtered_url = srs_string_trim_end(filtered_url, "/");
    
    // if empty, use root.
    if (filtered_url.empty()) {
        filtered_url = "/";
    }
    
    return _uri->initialize(filtered_url);
522 523
}
524 525
bool SrsHttpMessage::is_complete()
{
526 527 528 529 530 531 532 533 534 535
    return _state == SrsHttpParseStateComplete;
}

u_int8_t SrsHttpMessage::method()
{
    return (u_int8_t)_header.method;
}

string SrsHttpMessage::url()
{
536 537 538 539 540 541 542 543 544 545 546
    return _uri->get_url();
}

string SrsHttpMessage::path()
{
    return _uri->get_path();
}

string SrsHttpMessage::query()
{
    return _uri->get_query();
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
}

string SrsHttpMessage::body()
{
    std::string b;
    
    if (_body && !_body->empty()) {
        b.append(_body->bytes(), _body->size());
    }
    
    return b;
}

int64_t SrsHttpMessage::body_size()
{
    return (int64_t)_body->size();
}

int64_t SrsHttpMessage::content_length()
{
    return _header.content_length;
}
570 571 572 573 574
SrsHttpHandlerMatch* SrsHttpMessage::match()
{
    return _match;
}
575 576 577 578 579
bool SrsHttpMessage::requires_crossdomain()
{
    return _requires_crossdomain;
}
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
void SrsHttpMessage::set_url(std::string url)
{
    _url = url;
}

void SrsHttpMessage::set_state(SrsHttpParseState state)
{
    _state = state;
}

void SrsHttpMessage::set_header(http_parser* header)
{
    memcpy(&_header, header, sizeof(http_parser));
}
595 596 597 598 599 600
void SrsHttpMessage::set_match(SrsHttpHandlerMatch* match)
{
    srs_freep(_match);
    _match = match;
}
601 602 603 604 605
void SrsHttpMessage::set_requires_crossdomain(bool requires_crossdomain)
{
    _requires_crossdomain = requires_crossdomain;
}
606 607 608
void SrsHttpMessage::append_body(const char* body, int length)
{
    _body->append(body, length);
609 610 611 612
}

SrsHttpParser::SrsHttpParser()
{
613
    msg = NULL;
614 615 616 617
}

SrsHttpParser::~SrsHttpParser()
{
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
    srs_freep(msg);
}

int SrsHttpParser::initialize(enum http_parser_type type)
{
    int ret = ERROR_SUCCESS;
    
    memset(&settings, 0, sizeof(settings));
    settings.on_message_begin = on_message_begin;
    settings.on_url = on_url;
    settings.on_header_field = on_header_field;
    settings.on_header_value = on_header_value;
    settings.on_headers_complete = on_headers_complete;
    settings.on_body = on_body;
    settings.on_message_complete = on_message_complete;
    
    http_parser_init(&parser, type);
    // callback object ptr.
    parser.data = (void*)this;
    
    return ret;
}

int SrsHttpParser::parse_message(SrsSocket* skt, SrsHttpMessage** ppmsg)
{
    *ppmsg = NULL;
    
    int ret = ERROR_SUCCESS;

    // the msg must be always NULL
    srs_assert(msg == NULL);
    msg = new SrsHttpMessage();
    
    // reset response header.
    msg->reset();
    
    // do parse
    if ((ret = parse_message_imp(skt)) != ERROR_SUCCESS) {
        if (!srs_is_client_gracefully_close(ret)) {
            srs_error("parse http msg failed. ret=%d", ret);
        }
        srs_freep(msg);
        return ret;
    }
    
    // parse ok, return the msg.
    *ppmsg = msg;
    msg = NULL;
    
    return ret;
}

int SrsHttpParser::parse_message_imp(SrsSocket* skt)
{
    int ret = ERROR_SUCCESS;
    
    // the msg should never be NULL
    srs_assert(msg != NULL);
    
    // parser header.
    char buf[SRS_HTTP_HEADER_BUFFER];
    for (;;) {
        ssize_t nread;
        if ((ret = skt->read(buf, (size_t)sizeof(buf), &nread)) != ERROR_SUCCESS) {
            if (!srs_is_client_gracefully_close(ret)) {
                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 (msg->is_complete()) {
            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 SrsHttpParser::on_message_begin(http_parser* parser)
{
    SrsHttpParser* obj = (SrsHttpParser*)parser->data;
    obj->msg->set_state(SrsHttpParseStateStart);
    
    srs_info("***MESSAGE BEGIN***");
    
    return 0;
}

int SrsHttpParser::on_headers_complete(http_parser* parser)
{
    SrsHttpParser* obj = (SrsHttpParser*)parser->data;
    obj->msg->set_header(parser);
    
    srs_info("***HEADERS COMPLETE***");
    
    // see http_parser.c:1570, return 1 to skip body.
    return 0;
}

int SrsHttpParser::on_message_complete(http_parser* parser)
{
    SrsHttpParser* obj = (SrsHttpParser*)parser->data;
    // save the parser when header parse completed.
    obj->msg->set_state(SrsHttpParseStateComplete);
    
    srs_info("***MESSAGE COMPLETE***\n");
    
    return 0;
}

int SrsHttpParser::on_url(http_parser* parser, const char* at, size_t length)
{
    SrsHttpParser* obj = (SrsHttpParser*)parser->data;
    
    if (length > 0) {
        std::string url;
        
        url.append(at, (int)length);
        
        obj->msg->set_url(url);
    }
    
    srs_info("Method: %d, Url: %.*s", parser->method, (int)length, at);
    
    return 0;
}

int SrsHttpParser::on_header_field(http_parser* /*parser*/, const char* at, size_t length)
{
    srs_info("Header field: %.*s", (int)length, at);
    return 0;
}

int SrsHttpParser::on_header_value(http_parser* /*parser*/, const char* at, size_t length)
{
    srs_info("Header value: %.*s", (int)length, at);
    return 0;
}

int SrsHttpParser::on_body(http_parser* parser, const char* at, size_t length)
{
    SrsHttpParser* obj = (SrsHttpParser*)parser->data;
    
    if (length > 0) {
        srs_assert(obj);
        srs_assert(obj->msg);
        
        obj->msg->append_body(at, (int)length);
    }
    
    srs_info("Body: %.*s", (int)length, at);

    return 0;
781
}
782
winlin authored
783 784
SrsHttpUri::SrsHttpUri()
{
785
    port = SRS_DEFAULT_HTTP_PORT;
winlin authored
786 787 788 789 790 791 792 793
}

SrsHttpUri::~SrsHttpUri()
{
}

int SrsHttpUri::initialize(std::string _url)
{
794
    int ret = ERROR_SUCCESS;
winlin authored
795 796 797 798 799 800 801 802 803 804 805 806 807
    
    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;
    }
    
808
    std::string field = get_uri_field(url, &hp_u, UF_SCHEMA);
winlin authored
809 810 811 812 813 814 815 816 817 818 819 820 821 822
    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);
    
823
    query = get_uri_field(url, &hp_u, UF_QUERY);
824
    srs_info("parse query %s success", query.c_str());
825
    
826
    return ret;
winlin authored
827 828 829 830
}

const char* SrsHttpUri::get_url()
{
831
    return url.data();
winlin authored
832 833 834 835
}

const char* SrsHttpUri::get_schema()
{
836
    return schema.data();
winlin authored
837 838 839 840
}

const char* SrsHttpUri::get_host()
{
841
    return host.data();
winlin authored
842 843 844 845 846 847 848
}

int SrsHttpUri::get_port()
{
    return port;
}
849 850
const char* SrsHttpUri::get_path()
{
851 852 853 854 855 856
    return path.data();
}

const char* SrsHttpUri::get_query()
{
    return path.data();
857 858
}
winlin authored
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
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);
}
877
#endif