mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
support http error code, check url
This commit is contained in:
parent
0ae23d7556
commit
818c1f1520
5 changed files with 172 additions and 0 deletions
|
@ -34,6 +34,7 @@ using namespace std;
|
|||
#include <srs_app_socket.hpp>
|
||||
#include <srs_app_http_api.hpp>
|
||||
#include <srs_app_http_conn.hpp>
|
||||
#include <srs_app_json.hpp>
|
||||
|
||||
#define SRS_DEFAULT_HTTP_PORT 80
|
||||
|
||||
|
@ -85,10 +86,39 @@ int SrsHttpHandler::process_request(SrsSocket* skt, SrsHttpMessage* req)
|
|||
if (req->method() == HTTP_OPTIONS) {
|
||||
return res_options(skt);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
return res_error(skt, status_code, reason_phrase, ss.str());
|
||||
}
|
||||
|
||||
return do_process_request(skt, req);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int SrsHttpHandler::do_process_request(SrsSocket* /*skt*/, SrsHttpMessage* /*req*/)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
@ -156,6 +186,11 @@ int SrsHttpHandler::best_match(const char* path, int length, SrsHttpHandlerMatch
|
|||
(*ppmatch)->handler = handler;
|
||||
(*ppmatch)->matched_url.append(match_start, match_length);
|
||||
|
||||
int unmatch_length = length - match_length;
|
||||
if (unmatch_length > 0) {
|
||||
(*ppmatch)->unmatched_url.append(match_start + match_length, unmatch_length);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -166,6 +201,13 @@ SrsHttpHandler* SrsHttpHandler::res_status_line(std::stringstream& ss)
|
|||
return this;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
SrsHttpHandler* SrsHttpHandler::res_content_type(std::stringstream& ss)
|
||||
{
|
||||
ss << "Content-Type: text/html;charset=utf-8" << __CRLF
|
||||
|
@ -248,6 +290,22 @@ int SrsHttpHandler::res_json(SrsSocket* skt, std::string json)
|
|||
return res_flush(skt, ss);
|
||||
}
|
||||
|
||||
int SrsHttpHandler::res_error(SrsSocket* skt, int code, std::string reason_phrase, std::string body)
|
||||
{
|
||||
std::stringstream ss;
|
||||
|
||||
ss << "HTTP/1.1 " << code << " " << reason_phrase << __CRLF
|
||||
<< "Server: SRS/"RTMP_SIG_SRS_VERSION"" << __CRLF;
|
||||
|
||||
res_status_line_error(ss, code, reason_phrase)
|
||||
->res_content_type_json(ss)
|
||||
->res_content_length(ss, (int)body.length())
|
||||
->res_header_eof(ss)
|
||||
->res_body(ss, body);
|
||||
|
||||
return res_flush(skt, ss);
|
||||
}
|
||||
|
||||
SrsHttpHandler* SrsHttpHandler::create_http_api()
|
||||
{
|
||||
return new SrsApiRoot();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue