winlin

refine http client, use initialize to set host and port.

@@ -75,13 +75,18 @@ void SrsHttpHeartbeat::heartbeat() @@ -75,13 +75,18 @@ void SrsHttpHeartbeat::heartbeat()
75 } 75 }
76 ss << __SRS_JOBJECT_END; 76 ss << __SRS_JOBJECT_END;
77 77
78 - std::string data = ss.str(); 78 + std::string req = ss.str();
  79 +
79 SrsHttpClient http; 80 SrsHttpClient http;
  81 + if ((ret = http.initialize(uri.get_host(), uri.get_port())) != ERROR_SUCCESS) {
  82 + return;
  83 + }
  84 +
80 SrsHttpMessage* msg = NULL; 85 SrsHttpMessage* msg = NULL;
81 - if ((ret = http.post(&uri, data, &msg)) != ERROR_SUCCESS) { 86 + if ((ret = http.post(uri.get_path(), req, &msg)) != ERROR_SUCCESS) {
82 srs_info("http post hartbeart uri failed. " 87 srs_info("http post hartbeart uri failed. "
83 "url=%s, request=%s, response=%s, ret=%d", 88 "url=%s, request=%s, response=%s, ret=%d",
84 - url.c_str(), data.c_str(), res.c_str(), ret); 89 + url.c_str(), req.c_str(), res.c_str(), ret);
85 return; 90 return;
86 } 91 }
87 SrsAutoFree(SrsHttpMessage, msg); 92 SrsAutoFree(SrsHttpMessage, msg);
@@ -93,7 +98,7 @@ void SrsHttpHeartbeat::heartbeat() @@ -93,7 +98,7 @@ void SrsHttpHeartbeat::heartbeat()
93 98
94 srs_info("http hook hartbeart success. " 99 srs_info("http hook hartbeart success. "
95 "url=%s, request=%s, status_code=%d, response=%s, ret=%d", 100 "url=%s, request=%s, status_code=%d, response=%s, ret=%d",
96 - url.c_str(), data.c_str(), status_code, res.c_str(), ret); 101 + url.c_str(), req.c_str(), status_code, res.c_str(), ret);
97 102
98 return; 103 return;
99 } 104 }
@@ -1042,18 +1042,6 @@ int SrsHttpMessage::update(string url, http_parser* header, SrsFastBuffer* body, @@ -1042,18 +1042,6 @@ int SrsHttpMessage::update(string url, http_parser* header, SrsFastBuffer* body,
1042 1042
1043 // parse uri to schema/server:port/path?query 1043 // parse uri to schema/server:port/path?query
1044 std::string uri = "http://" + host + _url; 1044 std::string uri = "http://" + host + _url;
1045 -  
1046 - return update(uri);  
1047 -}  
1048 -  
1049 -int SrsHttpMessage::update(string uri)  
1050 -{  
1051 - int ret = ERROR_SUCCESS;  
1052 -  
1053 - if (uri.empty()) {  
1054 - return ret;  
1055 - }  
1056 -  
1057 if ((ret = _uri->initialize(uri)) != ERROR_SUCCESS) { 1045 if ((ret = _uri->initialize(uri)) != ERROR_SUCCESS) {
1058 return ret; 1046 return ret;
1059 } 1047 }
@@ -473,11 +473,6 @@ public: @@ -473,11 +473,6 @@ public:
473 virtual int update(std::string url, http_parser* header, 473 virtual int update(std::string url, http_parser* header,
474 SrsFastBuffer* body, std::vector<SrsHttpHeaderField>& headers 474 SrsFastBuffer* body, std::vector<SrsHttpHeaderField>& headers
475 ); 475 );
476 - /**  
477 - * update the request with uri.  
478 - * @remark user can invoke this multiple times.  
479 - */  
480 - virtual int update(std::string uri);  
481 public: 476 public:
482 virtual char* http_ts_send_buffer(); 477 virtual char* http_ts_send_buffer();
483 public: 478 public:
@@ -44,8 +44,8 @@ SrsHttpClient::SrsHttpClient() @@ -44,8 +44,8 @@ SrsHttpClient::SrsHttpClient()
44 { 44 {
45 connected = false; 45 connected = false;
46 stfd = NULL; 46 stfd = NULL;
47 - parser = NULL;  
48 skt = NULL; 47 skt = NULL;
  48 + parser = NULL;
49 } 49 }
50 50
51 SrsHttpClient::~SrsHttpClient() 51 SrsHttpClient::~SrsHttpClient()
@@ -54,22 +54,31 @@ SrsHttpClient::~SrsHttpClient() @@ -54,22 +54,31 @@ SrsHttpClient::~SrsHttpClient()
54 srs_freep(parser); 54 srs_freep(parser);
55 } 55 }
56 56
57 -int SrsHttpClient::post(SrsHttpUri* uri, string req, SrsHttpMessage** ppmsg) 57 +int SrsHttpClient::initialize(string h, int p)
58 { 58 {
59 - *ppmsg = NULL;  
60 -  
61 int ret = ERROR_SUCCESS; 59 int ret = ERROR_SUCCESS;
62 60
63 - if (!parser) {  
64 - parser = new SrsHttpParser();  
65 -  
66 - if ((ret = parser->initialize(HTTP_RESPONSE)) != ERROR_SUCCESS) {  
67 - srs_error("initialize parser failed. ret=%d", ret);  
68 - return ret;  
69 - } 61 + srs_freep(parser);
  62 + parser = new SrsHttpParser();
  63 +
  64 + if ((ret = parser->initialize(HTTP_RESPONSE)) != ERROR_SUCCESS) {
  65 + srs_error("initialize parser failed. ret=%d", ret);
  66 + return ret;
70 } 67 }
71 68
72 - if ((ret = connect(uri)) != ERROR_SUCCESS) { 69 + host = h;
  70 + port = p;
  71 +
  72 + return ret;
  73 +}
  74 +
  75 +int SrsHttpClient::post(string path, string req, SrsHttpMessage** ppmsg)
  76 +{
  77 + *ppmsg = NULL;
  78 +
  79 + int ret = ERROR_SUCCESS;
  80 +
  81 + if ((ret = connect()) != ERROR_SUCCESS) {
73 srs_warn("http connect server failed. ret=%d", ret); 82 srs_warn("http connect server failed. ret=%d", ret);
74 return ret; 83 return ret;
75 } 84 }
@@ -77,9 +86,9 @@ int SrsHttpClient::post(SrsHttpUri* uri, string req, SrsHttpMessage** ppmsg) @@ -77,9 +86,9 @@ int SrsHttpClient::post(SrsHttpUri* uri, string req, SrsHttpMessage** ppmsg)
77 // send POST request to uri 86 // send POST request to uri
78 // POST %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n\r\n%s 87 // POST %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n\r\n%s
79 std::stringstream ss; 88 std::stringstream ss;
80 - ss << "POST " << uri->get_path() << " " 89 + ss << "POST " << path << " "
81 << "HTTP/1.1" << __SRS_HTTP_CRLF 90 << "HTTP/1.1" << __SRS_HTTP_CRLF
82 - << "Host: " << uri->get_host() << __SRS_HTTP_CRLF 91 + << "Host: " << host << __SRS_HTTP_CRLF
83 << "Connection: Keep-Alive" << __SRS_HTTP_CRLF 92 << "Connection: Keep-Alive" << __SRS_HTTP_CRLF
84 << "Content-Length: " << std::dec << req.length() << __SRS_HTTP_CRLF 93 << "Content-Length: " << std::dec << req.length() << __SRS_HTTP_CRLF
85 << "User-Agent: " << RTMP_SIG_SRS_NAME << RTMP_SIG_SRS_VERSION << __SRS_HTTP_CRLF 94 << "User-Agent: " << RTMP_SIG_SRS_NAME << RTMP_SIG_SRS_VERSION << __SRS_HTTP_CRLF
@@ -109,22 +118,13 @@ int SrsHttpClient::post(SrsHttpUri* uri, string req, SrsHttpMessage** ppmsg) @@ -109,22 +118,13 @@ int SrsHttpClient::post(SrsHttpUri* uri, string req, SrsHttpMessage** ppmsg)
109 return ret; 118 return ret;
110 } 119 }
111 120
112 -int SrsHttpClient::get(SrsHttpUri* uri, std::string req, SrsHttpMessage** ppmsg) 121 +int SrsHttpClient::get(string path, std::string req, SrsHttpMessage** ppmsg)
113 { 122 {
114 *ppmsg = NULL; 123 *ppmsg = NULL;
115 124
116 int ret = ERROR_SUCCESS; 125 int ret = ERROR_SUCCESS;
117 126
118 - if (!parser) {  
119 - parser = new SrsHttpParser();  
120 -  
121 - if ((ret = parser->initialize(HTTP_RESPONSE)) != ERROR_SUCCESS) {  
122 - srs_error("initialize parser failed. ret=%d", ret);  
123 - return ret;  
124 - }  
125 - }  
126 -  
127 - if ((ret = connect(uri)) != ERROR_SUCCESS) { 127 + if ((ret = connect()) != ERROR_SUCCESS) {
128 srs_warn("http connect server failed. ret=%d", ret); 128 srs_warn("http connect server failed. ret=%d", ret);
129 return ret; 129 return ret;
130 } 130 }
@@ -132,9 +132,9 @@ int SrsHttpClient::get(SrsHttpUri* uri, std::string req, SrsHttpMessage** ppmsg) @@ -132,9 +132,9 @@ int SrsHttpClient::get(SrsHttpUri* uri, std::string req, SrsHttpMessage** ppmsg)
132 // send POST request to uri 132 // send POST request to uri
133 // GET %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n\r\n%s 133 // GET %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n\r\n%s
134 std::stringstream ss; 134 std::stringstream ss;
135 - ss << "GET " << uri->get_path() << " " 135 + ss << "GET " << path << " "
136 << "HTTP/1.1" << __SRS_HTTP_CRLF 136 << "HTTP/1.1" << __SRS_HTTP_CRLF
137 - << "Host: " << uri->get_host() << __SRS_HTTP_CRLF 137 + << "Host: " << host << __SRS_HTTP_CRLF
138 << "Connection: Keep-Alive" << __SRS_HTTP_CRLF 138 << "Connection: Keep-Alive" << __SRS_HTTP_CRLF
139 << "Content-Length: " << std::dec << req.length() << __SRS_HTTP_CRLF 139 << "Content-Length: " << std::dec << req.length() << __SRS_HTTP_CRLF
140 << "User-Agent: " << RTMP_SIG_SRS_NAME << RTMP_SIG_SRS_VERSION << __SRS_HTTP_CRLF 140 << "User-Agent: " << RTMP_SIG_SRS_NAME << RTMP_SIG_SRS_VERSION << __SRS_HTTP_CRLF
@@ -157,12 +157,6 @@ int SrsHttpClient::get(SrsHttpUri* uri, std::string req, SrsHttpMessage** ppmsg) @@ -157,12 +157,6 @@ int SrsHttpClient::get(SrsHttpUri* uri, std::string req, SrsHttpMessage** ppmsg)
157 return ret; 157 return ret;
158 } 158 }
159 srs_assert(msg); 159 srs_assert(msg);
160 -  
161 - // for GET, server response no uri, we update with request uri.  
162 - if ((ret = msg->update(uri->get_url())) != ERROR_SUCCESS) {  
163 - srs_freep(msg);  
164 - return ret;  
165 - }  
166 160
167 *ppmsg = msg; 161 *ppmsg = msg;
168 srs_info("parse http get response success."); 162 srs_info("parse http get response success.");
@@ -178,7 +172,7 @@ void SrsHttpClient::disconnect() @@ -178,7 +172,7 @@ void SrsHttpClient::disconnect()
178 srs_freep(skt); 172 srs_freep(skt);
179 } 173 }
180 174
181 -int SrsHttpClient::connect(SrsHttpUri* uri) 175 +int SrsHttpClient::connect()
182 { 176 {
183 int ret = ERROR_SUCCESS; 177 int ret = ERROR_SUCCESS;
184 178
@@ -188,18 +182,14 @@ int SrsHttpClient::connect(SrsHttpUri* uri) @@ -188,18 +182,14 @@ int SrsHttpClient::connect(SrsHttpUri* uri)
188 182
189 disconnect(); 183 disconnect();
190 184
191 - std::string server = uri->get_host();  
192 - int port = uri->get_port();  
193 -  
194 // open socket. 185 // open socket.
195 int64_t timeout = SRS_HTTP_CLIENT_SLEEP_US; 186 int64_t timeout = SRS_HTTP_CLIENT_SLEEP_US;
196 - if ((ret = srs_socket_connect(server, port, timeout, &stfd)) != ERROR_SUCCESS) { 187 + if ((ret = srs_socket_connect(host, port, timeout, &stfd)) != ERROR_SUCCESS) {
197 srs_warn("http client failed, server=%s, port=%d, timeout=%"PRId64", ret=%d", 188 srs_warn("http client failed, server=%s, port=%d, timeout=%"PRId64", ret=%d",
198 - server.c_str(), port, timeout, ret); 189 + host.c_str(), port, timeout, ret);
199 return ret; 190 return ret;
200 } 191 }
201 - srs_info("connect to server success. http url=%s, server=%s, port=%d",  
202 - uri->get_url(), uri->get_host(), uri->get_port()); 192 + srs_info("connect to server success. server=%s, port=%d", host, port);
203 193
204 srs_assert(!skt); 194 srs_assert(!skt);
205 skt = new SrsStSocket(stfd); 195 skt = new SrsStSocket(stfd);
@@ -50,25 +50,36 @@ private: @@ -50,25 +50,36 @@ private:
50 st_netfd_t stfd; 50 st_netfd_t stfd;
51 SrsStSocket* skt; 51 SrsStSocket* skt;
52 SrsHttpParser* parser; 52 SrsHttpParser* parser;
  53 +private:
  54 + // host name or ip.
  55 + std::string host;
  56 + int port;
53 public: 57 public:
54 SrsHttpClient(); 58 SrsHttpClient();
55 virtual ~SrsHttpClient(); 59 virtual ~SrsHttpClient();
56 public: 60 public:
57 /** 61 /**
  62 + * initialize the client, connect to host and port.
  63 + */
  64 + virtual int initialize(std::string h, int p);
  65 +public:
  66 + /**
58 * to post data to the uri. 67 * to post data to the uri.
  68 + * @param the path to request on.
59 * @param req the data post to uri. empty string to ignore. 69 * @param req the data post to uri. empty string to ignore.
60 * @param ppmsg output the http message to read the response. 70 * @param ppmsg output the http message to read the response.
61 */ 71 */
62 - virtual int post(SrsHttpUri* uri, std::string req, SrsHttpMessage** ppmsg); 72 + virtual int post(std::string path, std::string req, SrsHttpMessage** ppmsg);
63 /** 73 /**
64 * to get data from the uri. 74 * to get data from the uri.
  75 + * @param the path to request on.
65 * @param req the data post to uri. empty string to ignore. 76 * @param req the data post to uri. empty string to ignore.
66 * @param ppmsg output the http message to read the response. 77 * @param ppmsg output the http message to read the response.
67 */ 78 */
68 - virtual int get(SrsHttpUri* uri, std::string req, SrsHttpMessage** ppmsg); 79 + virtual int get(std::string path, std::string req, SrsHttpMessage** ppmsg);
69 private: 80 private:
70 virtual void disconnect(); 81 virtual void disconnect();
71 - virtual int connect(SrsHttpUri* uri); 82 + virtual int connect();
72 }; 83 };
73 84
74 #endif 85 #endif
@@ -312,8 +312,12 @@ int SrsHttpHooks::do_post(std::string url, std::string req, int& code, string& r @@ -312,8 +312,12 @@ int SrsHttpHooks::do_post(std::string url, std::string req, int& code, string& r
312 } 312 }
313 313
314 SrsHttpClient http; 314 SrsHttpClient http;
  315 + if ((ret = http.initialize(uri.get_host(), uri.get_port())) != ERROR_SUCCESS) {
  316 + return ret;
  317 + }
  318 +
315 SrsHttpMessage* msg = NULL; 319 SrsHttpMessage* msg = NULL;
316 - if ((ret = http.post(&uri, req, &msg)) != ERROR_SUCCESS) { 320 + if ((ret = http.post(uri.get_path(), req, &msg)) != ERROR_SUCCESS) {
317 return ret; 321 return ret;
318 } 322 }
319 SrsAutoFree(SrsHttpMessage, msg); 323 SrsAutoFree(SrsHttpMessage, msg);