2014-03-31 03:35:22 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) 2013-2014 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_app_http_conn.hpp>
|
|
|
|
|
2014-04-01 07:42:27 +00:00
|
|
|
#ifdef SRS_HTTP_SERVER
|
|
|
|
|
2014-04-01 06:28:19 +00:00
|
|
|
#include <sstream>
|
|
|
|
using namespace std;
|
|
|
|
|
2014-03-31 03:35:22 +00:00
|
|
|
#include <srs_kernel_log.hpp>
|
|
|
|
#include <srs_kernel_error.hpp>
|
2014-04-01 04:36:56 +00:00
|
|
|
#include <srs_app_socket.hpp>
|
2014-04-01 06:28:19 +00:00
|
|
|
#include <srs_app_http.hpp>
|
|
|
|
#include <srs_kernel_buffer.hpp>
|
2014-04-02 04:03:49 +00:00
|
|
|
#include <srs_core_autofree.hpp>
|
2014-04-01 04:36:56 +00:00
|
|
|
|
|
|
|
#define SRS_HTTP_HEADER_BUFFER 1024
|
2014-03-31 03:35:22 +00:00
|
|
|
|
2014-04-02 10:07:34 +00:00
|
|
|
SrsHttpConn::SrsHttpConn(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler)
|
2014-03-31 03:35:22 +00:00
|
|
|
: SrsConnection(srs_server, client_stfd)
|
|
|
|
{
|
2014-04-02 04:03:49 +00:00
|
|
|
parser = new SrsHttpParser();
|
2014-04-02 10:07:34 +00:00
|
|
|
handler = _handler;
|
2014-03-31 03:35:22 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 06:28:19 +00:00
|
|
|
SrsHttpConn::~SrsHttpConn()
|
2014-03-31 03:35:22 +00:00
|
|
|
{
|
2014-04-02 04:03:49 +00:00
|
|
|
srs_freep(parser);
|
2014-03-31 03:35:22 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 06:28:19 +00:00
|
|
|
int SrsHttpConn::do_cycle()
|
2014-03-31 03:35:22 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
if ((ret = get_peer_ip()) != ERROR_SUCCESS) {
|
|
|
|
srs_error("get peer ip failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
srs_trace("http get peer ip success. ip=%s", ip);
|
2014-04-01 04:36:56 +00:00
|
|
|
|
2014-04-02 04:03:49 +00:00
|
|
|
// initialize parser
|
|
|
|
if ((ret = parser->initialize(HTTP_REQUEST)) != ERROR_SUCCESS) {
|
2014-04-02 04:55:10 +00:00
|
|
|
srs_error("http initialize http parser failed. ret=%d", ret);
|
2014-04-02 04:03:49 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2014-04-01 04:36:56 +00:00
|
|
|
|
|
|
|
// underlayer socket
|
|
|
|
SrsSocket skt(stfd);
|
|
|
|
|
2014-04-02 04:03:49 +00:00
|
|
|
// process http messages.
|
2014-04-01 04:36:56 +00:00
|
|
|
for (;;) {
|
2014-04-02 04:03:49 +00:00
|
|
|
SrsHttpMessage* req = NULL;
|
2014-04-01 04:36:56 +00:00
|
|
|
|
2014-04-02 04:03:49 +00:00
|
|
|
// get a http message
|
|
|
|
if ((ret = parser->parse_message(&skt, &req)) != ERROR_SUCCESS) {
|
2014-04-01 04:36:56 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-02 04:03:49 +00:00
|
|
|
// if SUCCESS, always NOT-NULL and completed message.
|
|
|
|
srs_assert(req);
|
|
|
|
srs_assert(req->is_complete());
|
|
|
|
|
|
|
|
// always free it in this scope.
|
|
|
|
SrsAutoFree(SrsHttpMessage, req, false);
|
2014-04-01 04:36:56 +00:00
|
|
|
|
2014-04-02 04:03:49 +00:00
|
|
|
// ok, handle http request.
|
|
|
|
if ((ret = process_request(&skt, req)) != ERROR_SUCCESS) {
|
2014-04-01 04:36:56 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2014-04-02 04:03:49 +00:00
|
|
|
|
2014-04-01 04:36:56 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-02 04:03:49 +00:00
|
|
|
int SrsHttpConn::process_request(SrsSocket* skt, SrsHttpMessage* req)
|
2014-04-01 04:36:56 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
2014-03-31 03:35:22 +00:00
|
|
|
|
2014-04-02 04:03:49 +00:00
|
|
|
if (req->method() == HTTP_OPTIONS) {
|
2014-04-01 06:28:19 +00:00
|
|
|
char data[] = "HTTP/1.1 200 OK" __CRLF
|
|
|
|
"Content-Length: 0"__CRLF
|
|
|
|
"Server: SRS/"RTMP_SIG_SRS_VERSION""__CRLF
|
|
|
|
"Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT"__CRLF
|
|
|
|
"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
|
|
|
|
"Content-Type: text/html;charset=utf-8"__CRLFCRLF
|
|
|
|
"";
|
|
|
|
return skt->write(data, sizeof(data), NULL);
|
|
|
|
} else {
|
|
|
|
std::string tilte = "SRS/"RTMP_SIG_SRS_VERSION;
|
2014-04-02 04:55:10 +00:00
|
|
|
tilte += " hello http/1.1 server~\n";
|
2014-03-31 03:35:22 +00:00
|
|
|
|
2014-04-01 06:28:19 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "HTTP/1.1 200 OK " << __CRLF
|
2014-04-02 04:03:49 +00:00
|
|
|
<< "Content-Length: "<< tilte.length() + req->body_size() << __CRLF
|
2014-04-01 06:28:19 +00:00
|
|
|
<< "Server: SRS/"RTMP_SIG_SRS_VERSION"" << __CRLF
|
|
|
|
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __CRLF
|
|
|
|
<< "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
|
|
|
|
<< "Content-Type: text/html;charset=utf-8" << __CRLFCRLF
|
2014-04-02 04:03:49 +00:00
|
|
|
<< tilte << req->body().c_str()
|
2014-04-01 06:28:19 +00:00
|
|
|
<< "";
|
|
|
|
return skt->write(ss.str().c_str(), ss.str().length(), NULL);
|
|
|
|
}
|
2014-04-01 04:36:56 +00:00
|
|
|
|
2014-03-31 03:35:22 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2014-04-01 04:36:56 +00:00
|
|
|
|
2014-04-01 07:42:27 +00:00
|
|
|
#endif
|