1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

Refactor header of HTTP message by using SrsHttpHeader.

This commit is contained in:
winlin 2019-12-16 16:00:02 +08:00
parent dcb7b6aae0
commit ca2b68f428
6 changed files with 95 additions and 120 deletions

View file

@ -173,6 +173,20 @@ void SrsHttpHeader::del(string key)
}
}
int SrsHttpHeader::count()
{
return (int)headers.size();
}
void SrsHttpHeader::dumps(SrsJsonObject* o)
{
map<string, string>::iterator it;
for (it = headers.begin(); it != headers.end(); ++it) {
string v = it->second;
o->set(it->first, SrsJsonAny::str(v.c_str()));
}
}
int64_t SrsHttpHeader::content_length()
{
std::string cl = get("Content-Length");
@ -776,13 +790,8 @@ srs_error_t SrsHttpCorsMux::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessag
// If CORS enabled, and there is a "Origin" header, it's CORS.
if (enabled) {
for (int i = 0; i < r->request_header_count(); i++) {
string k = r->request_header_key_at(i);
if (k == "Origin" || k == "origin") {
required = true;
break;
}
}
SrsHttpHeader* h = r->header();
required = !h->get("Origin").empty();
}
// When CORS required, set the CORS headers.

View file

@ -45,6 +45,7 @@ class SrsHttpHeader;
class ISrsHttpMessage;
class SrsHttpMuxEntry;
class ISrsHttpResponseWriter;
class SrsJsonObject;
// From http specification
// CR = <US-ASCII CR, carriage return (13)>
@ -129,6 +130,11 @@ public:
// Delete the http header indicated by key.
// Return the removed header field.
virtual void del(std::string);
// Get the count of headers.
virtual int count();
public:
// Dumps to a JSON object.
virtual void dumps(SrsJsonObject* o);
public:
// Get the content length. -1 if not set.
virtual int64_t content_length();
@ -505,9 +511,7 @@ public:
// then query_get("start") is "100", and query_get("end") is "200"
virtual std::string query_get(std::string key) = 0;
// Get the headers.
virtual int request_header_count() = 0;
virtual std::string request_header_key_at(int index) = 0;
virtual std::string request_header_value_at(int index) = 0;
virtual SrsHttpHeader* header() = 0;
public:
// Whether the current request is JSONP,
// which has a "callback=xxx" in QueryString.