Blame view

trunk/src/app/srs_app_http_client.cpp 5.5 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
winlin authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

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_app_http_client.hpp>
26 27 28 29 30 31 32 33 34
#ifdef SRS_AUTO_HTTP_PARSER

#include <arpa/inet.h>

using namespace std;

#include <srs_app_http.hpp>
#include <srs_kernel_error.hpp>
#include <srs_kernel_log.hpp>
35
#include <srs_app_st_socket.hpp>
36
#include <srs_kernel_utility.hpp>
37
#include <srs_app_utility.hpp>
38
#include <srs_core_autofree.hpp>
39
40 41 42 43
SrsHttpClient::SrsHttpClient()
{
    connected = false;
    stfd = NULL;
winlin authored
44
    skt = NULL;
45
    parser = NULL;
46
    timeout_us = 0;
47 48 49 50 51 52 53 54
}

SrsHttpClient::~SrsHttpClient()
{
    disconnect();
    srs_freep(parser);
}
55
int SrsHttpClient::initialize(string h, int p, int64_t t_us)
56 57 58
{
    int ret = ERROR_SUCCESS;
    
59 60 61 62 63 64
    srs_freep(parser);
    parser = new SrsHttpParser();
    
    if ((ret = parser->initialize(HTTP_RESPONSE)) != ERROR_SUCCESS) {
        srs_error("initialize parser failed. ret=%d", ret);
        return ret;
65 66
    }
    
67 68
    host = h;
    port = p;
69
    timeout_us = t_us;
70 71 72 73 74 75 76 77 78 79 80
    
    return ret;
}

int SrsHttpClient::post(string path, string req, SrsHttpMessage** ppmsg)
{
    *ppmsg = NULL;
    
    int ret = ERROR_SUCCESS;
    
    if ((ret = connect()) != ERROR_SUCCESS) {
81
        srs_warn("http connect server failed. ret=%d", ret);
82 83 84 85 86 87
        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;
88
    ss << "POST " << path << " "
89 90 91 92 93 94 95
        << "HTTP/1.1" << SRS_HTTP_CRLF
        << "Host: " << host << SRS_HTTP_CRLF
        << "Connection: Keep-Alive" << SRS_HTTP_CRLF
        << "Content-Length: " << std::dec << req.length() << SRS_HTTP_CRLF
        << "User-Agent: " << RTMP_SIG_SRS_NAME << RTMP_SIG_SRS_VERSION << SRS_HTTP_CRLF
        << "Content-Type: application/json" << SRS_HTTP_CRLF
        << SRS_HTTP_CRLF
96 97 98
        << req;
    
    std::string data = ss.str();
winlin authored
99
    if ((ret = skt->write((void*)data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) {
100 101 102 103 104 105 106 107
        // disconnect when error.
        disconnect();
        
        srs_error("write http post failed. ret=%d", ret);
        return ret;
    }
    
    SrsHttpMessage* msg = NULL;
winlin authored
108
    if ((ret = parser->parse_message(skt, &msg)) != ERROR_SUCCESS) {
109 110 111 112 113
        srs_error("parse http post response failed. ret=%d", ret);
        return ret;
    }

    srs_assert(msg);
114
    *ppmsg = msg;
115 116 117 118 119
    srs_info("parse http post response success.");
    
    return ret;
}
120
int SrsHttpClient::get(string path, std::string req, SrsHttpMessage** ppmsg)
winlin authored
121 122 123 124 125
{
    *ppmsg = NULL;

    int ret = ERROR_SUCCESS;
126
    if ((ret = connect()) != ERROR_SUCCESS) {
winlin authored
127 128 129 130 131 132 133
        srs_warn("http connect server failed. ret=%d", ret);
        return ret;
    }

    // send POST request to uri
    // GET %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n\r\n%s
    std::stringstream ss;
134
    ss << "GET " << path << " "
135 136 137 138 139 140 141
        << "HTTP/1.1" << SRS_HTTP_CRLF
        << "Host: " << host << SRS_HTTP_CRLF
        << "Connection: Keep-Alive" << SRS_HTTP_CRLF
        << "Content-Length: " << std::dec << req.length() << SRS_HTTP_CRLF
        << "User-Agent: " << RTMP_SIG_SRS_NAME << RTMP_SIG_SRS_VERSION << SRS_HTTP_CRLF
        << "Content-Type: application/json" << SRS_HTTP_CRLF
        << SRS_HTTP_CRLF
winlin authored
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
        << req;

    std::string data = ss.str();
    if ((ret = skt->write((void*)data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) {
        // disconnect when error.
        disconnect();

        srs_error("write http get failed. ret=%d", ret);
        return ret;
    }

    SrsHttpMessage* msg = NULL;
    if ((ret = parser->parse_message(skt, &msg)) != ERROR_SUCCESS) {
        srs_error("parse http post response failed. ret=%d", ret);
        return ret;
    }
    srs_assert(msg);
159
winlin authored
160 161 162 163 164 165
    *ppmsg = msg;
    srs_info("parse http get response success.");

    return ret;
}
166 167 168 169 170
void SrsHttpClient::disconnect()
{
    connected = false;
    
    srs_close_stfd(stfd);
winlin authored
171
    srs_freep(skt);
172 173
}
174
int SrsHttpClient::connect()
175 176 177 178 179 180 181 182 183
{
    int ret = ERROR_SUCCESS;
    
    if (connected) {
        return ret;
    }
    
    disconnect();
    
184
    // open socket.
185
    if ((ret = srs_socket_connect(host, port, timeout_us, &stfd)) != ERROR_SUCCESS) {
186
        srs_warn("http client failed, server=%s, port=%d, timeout=%"PRId64", ret=%d",
187
            host.c_str(), port, timeout_us, ret);
188 189
        return ret;
    }
190
    srs_info("connect to server success. server=%s, port=%d", host, port);
191
    
winlin authored
192 193
    srs_assert(!skt);
    skt = new SrsStSocket(stfd);
194 195
    connected = true;
    
196 197 198 199
    // set the recv/send timeout in us.
    skt->set_recv_timeout(timeout_us);
    skt->set_send_timeout(timeout_us);
    
200 201 202 203
    return ret;
}

#endif
204