Toggle navigation
Toggle navigation
此项目
正在载入...
Sign in
胡斌
/
srs
转到一个项目
Toggle navigation
项目
群组
代码片段
帮助
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
winlin
2015-03-06 12:07:12 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
1277968d4a543deee240b804a3b53be2efe635aa
1277968d
1 parent
2bcb4f81
refine http client, use initialize to set host and port.
隐藏空白字符变更
内嵌
并排对比
正在显示
6 个修改的文件
包含
59 行增加
和
66 行删除
trunk/src/app/srs_app_heartbeat.cpp
trunk/src/app/srs_app_http.cpp
trunk/src/app/srs_app_http.hpp
trunk/src/app/srs_app_http_client.cpp
trunk/src/app/srs_app_http_client.hpp
trunk/src/app/srs_app_http_hooks.cpp
trunk/src/app/srs_app_heartbeat.cpp
查看文件 @
1277968
...
...
@@ -75,13 +75,18 @@ void SrsHttpHeartbeat::heartbeat()
}
ss
<<
__SRS_JOBJECT_END
;
std
::
string
data
=
ss
.
str
();
std
::
string
req
=
ss
.
str
();
SrsHttpClient
http
;
if
((
ret
=
http
.
initialize
(
uri
.
get_host
(),
uri
.
get_port
()))
!=
ERROR_SUCCESS
)
{
return
;
}
SrsHttpMessage
*
msg
=
NULL
;
if
((
ret
=
http
.
post
(
&
uri
,
data
,
&
msg
))
!=
ERROR_SUCCESS
)
{
if
((
ret
=
http
.
post
(
uri
.
get_path
(),
req
,
&
msg
))
!=
ERROR_SUCCESS
)
{
srs_info
(
"http post hartbeart uri failed. "
"url=%s, request=%s, response=%s, ret=%d"
,
url
.
c_str
(),
data
.
c_str
(),
res
.
c_str
(),
ret
);
url
.
c_str
(),
req
.
c_str
(),
res
.
c_str
(),
ret
);
return
;
}
SrsAutoFree
(
SrsHttpMessage
,
msg
);
...
...
@@ -93,7 +98,7 @@ void SrsHttpHeartbeat::heartbeat()
srs_info
(
"http hook hartbeart success. "
"url=%s, request=%s, status_code=%d, response=%s, ret=%d"
,
url
.
c_str
(),
data
.
c_str
(),
status_code
,
res
.
c_str
(),
ret
);
url
.
c_str
(),
req
.
c_str
(),
status_code
,
res
.
c_str
(),
ret
);
return
;
}
...
...
trunk/src/app/srs_app_http.cpp
查看文件 @
1277968
...
...
@@ -1042,18 +1042,6 @@ int SrsHttpMessage::update(string url, http_parser* header, SrsFastBuffer* body,
// parse uri to schema/server:port/path?query
std
::
string
uri
=
"http://"
+
host
+
_url
;
return
update
(
uri
);
}
int
SrsHttpMessage
::
update
(
string
uri
)
{
int
ret
=
ERROR_SUCCESS
;
if
(
uri
.
empty
())
{
return
ret
;
}
if
((
ret
=
_uri
->
initialize
(
uri
))
!=
ERROR_SUCCESS
)
{
return
ret
;
}
...
...
trunk/src/app/srs_app_http.hpp
查看文件 @
1277968
...
...
@@ -473,11 +473,6 @@ public:
virtual
int
update
(
std
::
string
url
,
http_parser
*
header
,
SrsFastBuffer
*
body
,
std
::
vector
<
SrsHttpHeaderField
>&
headers
);
/**
* update the request with uri.
* @remark user can invoke this multiple times.
*/
virtual
int
update
(
std
::
string
uri
);
public
:
virtual
char
*
http_ts_send_buffer
();
public
:
...
...
trunk/src/app/srs_app_http_client.cpp
查看文件 @
1277968
...
...
@@ -44,8 +44,8 @@ SrsHttpClient::SrsHttpClient()
{
connected
=
false
;
stfd
=
NULL
;
parser
=
NULL
;
skt
=
NULL
;
parser
=
NULL
;
}
SrsHttpClient
::~
SrsHttpClient
()
...
...
@@ -54,22 +54,31 @@ SrsHttpClient::~SrsHttpClient()
srs_freep
(
parser
);
}
int
SrsHttpClient
::
post
(
SrsHttpUri
*
uri
,
string
req
,
SrsHttpMessage
**
ppmsg
)
int
SrsHttpClient
::
initialize
(
string
h
,
int
p
)
{
*
ppmsg
=
NULL
;
int
ret
=
ERROR_SUCCESS
;
if
(
!
parser
)
{
parser
=
new
SrsHttpParser
();
if
((
ret
=
parser
->
initialize
(
HTTP_RESPONSE
))
!=
ERROR_SUCCESS
)
{
srs_error
(
"initialize parser failed. ret=%d"
,
ret
);
return
ret
;
}
srs_freep
(
parser
);
parser
=
new
SrsHttpParser
();
if
((
ret
=
parser
->
initialize
(
HTTP_RESPONSE
))
!=
ERROR_SUCCESS
)
{
srs_error
(
"initialize parser failed. ret=%d"
,
ret
);
return
ret
;
}
if
((
ret
=
connect
(
uri
))
!=
ERROR_SUCCESS
)
{
host
=
h
;
port
=
p
;
return
ret
;
}
int
SrsHttpClient
::
post
(
string
path
,
string
req
,
SrsHttpMessage
**
ppmsg
)
{
*
ppmsg
=
NULL
;
int
ret
=
ERROR_SUCCESS
;
if
((
ret
=
connect
())
!=
ERROR_SUCCESS
)
{
srs_warn
(
"http connect server failed. ret=%d"
,
ret
);
return
ret
;
}
...
...
@@ -77,9 +86,9 @@ int SrsHttpClient::post(SrsHttpUri* uri, string req, SrsHttpMessage** ppmsg)
// 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
()
<<
" "
ss
<<
"POST "
<<
path
<<
" "
<<
"HTTP/1.1"
<<
__SRS_HTTP_CRLF
<<
"Host: "
<<
uri
->
get_host
()
<<
__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
...
...
@@ -109,22 +118,13 @@ int SrsHttpClient::post(SrsHttpUri* uri, string req, SrsHttpMessage** ppmsg)
return
ret
;
}
int
SrsHttpClient
::
get
(
SrsHttpUri
*
uri
,
std
::
string
req
,
SrsHttpMessage
**
ppmsg
)
int
SrsHttpClient
::
get
(
string
path
,
std
::
string
req
,
SrsHttpMessage
**
ppmsg
)
{
*
ppmsg
=
NULL
;
int
ret
=
ERROR_SUCCESS
;
if
(
!
parser
)
{
parser
=
new
SrsHttpParser
();
if
((
ret
=
parser
->
initialize
(
HTTP_RESPONSE
))
!=
ERROR_SUCCESS
)
{
srs_error
(
"initialize parser failed. ret=%d"
,
ret
);
return
ret
;
}
}
if
((
ret
=
connect
(
uri
))
!=
ERROR_SUCCESS
)
{
if
((
ret
=
connect
())
!=
ERROR_SUCCESS
)
{
srs_warn
(
"http connect server failed. ret=%d"
,
ret
);
return
ret
;
}
...
...
@@ -132,9 +132,9 @@ int SrsHttpClient::get(SrsHttpUri* uri, std::string req, SrsHttpMessage** ppmsg)
// 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
;
ss
<<
"GET "
<<
uri
->
get_path
()
<<
" "
ss
<<
"GET "
<<
path
<<
" "
<<
"HTTP/1.1"
<<
__SRS_HTTP_CRLF
<<
"Host: "
<<
uri
->
get_host
()
<<
__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
...
...
@@ -157,12 +157,6 @@ int SrsHttpClient::get(SrsHttpUri* uri, std::string req, SrsHttpMessage** ppmsg)
return
ret
;
}
srs_assert
(
msg
);
// for GET, server response no uri, we update with request uri.
if
((
ret
=
msg
->
update
(
uri
->
get_url
()))
!=
ERROR_SUCCESS
)
{
srs_freep
(
msg
);
return
ret
;
}
*
ppmsg
=
msg
;
srs_info
(
"parse http get response success."
);
...
...
@@ -178,7 +172,7 @@ void SrsHttpClient::disconnect()
srs_freep
(
skt
);
}
int
SrsHttpClient
::
connect
(
SrsHttpUri
*
uri
)
int
SrsHttpClient
::
connect
()
{
int
ret
=
ERROR_SUCCESS
;
...
...
@@ -188,18 +182,14 @@ int SrsHttpClient::connect(SrsHttpUri* uri)
disconnect
();
std
::
string
server
=
uri
->
get_host
();
int
port
=
uri
->
get_port
();
// open socket.
int64_t
timeout
=
SRS_HTTP_CLIENT_SLEEP_US
;
if
((
ret
=
srs_socket_connect
(
server
,
port
,
timeout
,
&
stfd
))
!=
ERROR_SUCCESS
)
{
if
((
ret
=
srs_socket_connect
(
host
,
port
,
timeout
,
&
stfd
))
!=
ERROR_SUCCESS
)
{
srs_warn
(
"http client failed, server=%s, port=%d, timeout=%"
PRId64
", ret=%d"
,
server
.
c_str
(),
port
,
timeout
,
ret
);
host
.
c_str
(),
port
,
timeout
,
ret
);
return
ret
;
}
srs_info
(
"connect to server success. http url=%s, server=%s, port=%d"
,
uri
->
get_url
(),
uri
->
get_host
(),
uri
->
get_port
());
srs_info
(
"connect to server success. server=%s, port=%d"
,
host
,
port
);
srs_assert
(
!
skt
);
skt
=
new
SrsStSocket
(
stfd
);
...
...
trunk/src/app/srs_app_http_client.hpp
查看文件 @
1277968
...
...
@@ -50,25 +50,36 @@ private:
st_netfd_t
stfd
;
SrsStSocket
*
skt
;
SrsHttpParser
*
parser
;
private
:
// host name or ip.
std
::
string
host
;
int
port
;
public
:
SrsHttpClient
();
virtual
~
SrsHttpClient
();
public
:
/**
* initialize the client, connect to host and port.
*/
virtual
int
initialize
(
std
::
string
h
,
int
p
);
public
:
/**
* to post data to the uri.
* @param the path to request on.
* @param req the data post to uri. empty string to ignore.
* @param ppmsg output the http message to read the response.
*/
virtual
int
post
(
SrsHttpUri
*
uri
,
std
::
string
req
,
SrsHttpMessage
**
ppmsg
);
virtual
int
post
(
std
::
string
path
,
std
::
string
req
,
SrsHttpMessage
**
ppmsg
);
/**
* to get data from the uri.
* @param the path to request on.
* @param req the data post to uri. empty string to ignore.
* @param ppmsg output the http message to read the response.
*/
virtual
int
get
(
SrsHttpUri
*
uri
,
std
::
string
req
,
SrsHttpMessage
**
ppmsg
);
virtual
int
get
(
std
::
string
path
,
std
::
string
req
,
SrsHttpMessage
**
ppmsg
);
private
:
virtual
void
disconnect
();
virtual
int
connect
(
SrsHttpUri
*
uri
);
virtual
int
connect
();
};
#endif
...
...
trunk/src/app/srs_app_http_hooks.cpp
查看文件 @
1277968
...
...
@@ -312,8 +312,12 @@ int SrsHttpHooks::do_post(std::string url, std::string req, int& code, string& r
}
SrsHttpClient
http
;
if
((
ret
=
http
.
initialize
(
uri
.
get_host
(),
uri
.
get_port
()))
!=
ERROR_SUCCESS
)
{
return
ret
;
}
SrsHttpMessage
*
msg
=
NULL
;
if
((
ret
=
http
.
post
(
&
uri
,
req
,
&
msg
))
!=
ERROR_SUCCESS
)
{
if
((
ret
=
http
.
post
(
uri
.
get_path
()
,
req
,
&
msg
))
!=
ERROR_SUCCESS
)
{
return
ret
;
}
SrsAutoFree
(
SrsHttpMessage
,
msg
);
...
...
请
注册
或
登录
后发表评论