Skip to content

Commit

Permalink
for ossrs#293, ossrs#277, support http chunked encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jan 18, 2015
1 parent 15d8753 commit b04888a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
45 changes: 42 additions & 3 deletions trunk/src/app/srs_app_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ int SrsGoHttpFileServer::serve_file(ISrsGoHttpResponseWriter* w, SrsHttpMessage*

int64_t length = fs.filesize();

w->header()->set_content_length(length);
// unset the content length in chunked encoding.
//w->header()->set_content_length(length);

static std::map<std::string, std::string> _mime;
if (_mime.empty()) {
Expand Down Expand Up @@ -391,7 +392,7 @@ int SrsGoHttpFileServer::serve_file(ISrsGoHttpResponseWriter* w, SrsHttpMessage*
return ret;
}

return ret;
return w->final_request();
}

int SrsGoHttpFileServer::serve_flv_stream(ISrsGoHttpResponseWriter* w, SrsHttpMessage* r, string fullpath, int offset)
Expand Down Expand Up @@ -653,6 +654,20 @@ SrsGoHttpResponseWriter::~SrsGoHttpResponseWriter()
srs_freep(hdr);
}

int SrsGoHttpResponseWriter::final_request()
{
// complete the chunked encoding.
if (content_length == -1) {
std::stringstream ss;
ss << 0 << __SRS_CRLF << __SRS_CRLF;
std::string ch = ss.str();
return skt->write((void*)ch.data(), (int)ch.length(), NULL);
}

// ignore when send with content length
return ERROR_SUCCESS;
}

SrsGoHttpHeader* SrsGoHttpResponseWriter::header()
{
return hdr;
Expand All @@ -678,7 +693,26 @@ int SrsGoHttpResponseWriter::write(char* data, int size)
return ret;
}

return skt->write((void*)data, size, NULL);
// directly send with content length
if (content_length != -1) {
return skt->write((void*)data, size, NULL);
}

// send in chunked encoding.
std::stringstream ss;
ss << hex << size << __SRS_CRLF;
std::string ch = ss.str();
if ((ret = skt->write((void*)ch.data(), (int)ch.length(), NULL)) != ERROR_SUCCESS) {
return ret;
}
if ((ret = skt->write((void*)data, size, NULL)) != ERROR_SUCCESS) {
return ret;
}
if ((ret = skt->write((void*)__SRS_CRLF, 2, NULL)) != ERROR_SUCCESS) {
return ret;
}

return ret;
}

void SrsGoHttpResponseWriter::write_header(int code)
Expand Down Expand Up @@ -722,6 +756,11 @@ int SrsGoHttpResponseWriter::send_header(char* data, int size)
hdr->set("Server", RTMP_SIG_SRS_KEY"/"RTMP_SIG_SRS_VERSION);
}

// chunked encoding
if (content_length == -1) {
hdr->set("Transfer-Encoding", "chunked");
}

// write headers
hdr->write(ss);

Expand Down
5 changes: 5 additions & 0 deletions trunk/src/app/srs_app_http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ class ISrsGoHttpResponseWriter
ISrsGoHttpResponseWriter();
virtual ~ISrsGoHttpResponseWriter();
public:
// when chunked mode,
// final the request to complete the chunked encoding.
virtual int final_request() = 0;

// Header returns the header map that will be sent by WriteHeader.
// Changing the header after a call to WriteHeader (or Write) has
// no effect.
Expand Down Expand Up @@ -319,6 +323,7 @@ class SrsGoHttpResponseWriter : public ISrsGoHttpResponseWriter
SrsGoHttpResponseWriter(SrsStSocket* io);
virtual ~SrsGoHttpResponseWriter();
public:
virtual int final_request();
virtual SrsGoHttpHeader* header();
virtual int write(char* data, int size);
virtual void write_header(int code);
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_http_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ int SrsLiveStream::serve_http(ISrsGoHttpResponseWriter* w, SrsHttpMessage* r)
// TODO: FIMXE: add pithy print.

// write http header for streaming.
w->header()->set_content_length((int64_t)2 * 1024 * 1024 * 1024);
// use chunked encoding, for we donot specifes the content length.
if (serve_flv_streaming) {
w->header()->set_content_type("video/x-flv");
}
Expand Down

0 comments on commit b04888a

Please sign in to comment.