summaryrefslogtreecommitdiff
path: root/src/Http.cpp
blob: 7746cc960e1c45657d5778e9d1fd47c6347d61e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Http.cpp
 *
 *  Created on: 2014. 6. 18.
 *      Author: oskwon
 */

#include <string.h>

#include <sstream>

#include "Util.h"
#include "Logger.h"

#include "Http.h"
#include "UriDecoder.h"

using namespace std;
//----------------------------------------------------------------------

bool HttpHeader::parse_request(std::string header)
{
	std::string line, key, value;
	std::istringstream request_stream;
	request_stream.str(header);

	request_stream >> method;
	request_stream >> path;
	request_stream >> version;
	std::getline(request_stream, line);

	while(std::getline(request_stream, line)) {
		if ((line = Util::trim(line)) != "") {
			Util::split_key_value(line, ":", key, value);

			key   = Util::trim(key);
			value = Util::trim(value);

			params[key] = value;
			DEBUG("add param : [%s] - [%s]", key.c_str(), value.c_str());
		}
	}

	int idx = path.find("?");
	// page
	if (idx != std::string::npos) {
		page = path.substr(0,idx);
		std::string page_param = path.substr(idx + 1);

		DEBUG("request url : [%s] - [%s]", page.c_str(), page_param.c_str());
		std::istringstream request_params_stream;
		request_params_stream.str(page_param);
		while(std::getline(request_params_stream, line, '&')) {
			if ((line = Util::trim(line)) != "") {
				Util::split_key_value(line, "=", key, value);

				key   = Util::trim(key);
				value = Util::trim(value);

				page_params[key] = value;
				DEBUG("add page param : [%s] - [%s]", key.c_str(), value.c_str());
			}
		}

		if (page == "/file") {
			type = HttpHeader::TRANSCODING_FILE;
		}
		else if (page == "/m3u") {
			type = HttpHeader::M3U;
		}
	}
	// live
	else {
		type = HttpHeader::TRANSCODING_LIVE;
	}
	return true;
}
//----------------------------------------------------------------------

static const char *http_ok          = "HTTP/1.1 200 OK\r\n";
static const char *http_partial     = "HTTP/1.1 206 Partial Content\r\n";
static const char *http_connection  = "Connection: Close\r\n";
static const char *http_server      = "Server: transtreamproxy\r\n";
static const char *http_done        = "\r\n";
std::string HttpHeader::build_response(Mpeg *source)
{
	std::ostringstream oss;

	switch(type) {
	case HttpHeader::TRANSCODING_FILE: {
			std::string range = params["Range"];
			off_t seek_offset = 0, content_length = 0;

			if((range.length() > 7) && (range.substr(0, 6) == "bytes=")) {
				range = range.substr(6);
				if(range.find('-') == (range.length() - 1)) {
					seek_offset = Util::strtollu(range);
				}
			}

			content_length = source->stream_length - seek_offset;
			if (seek_offset > 0) {
				content_length += 1;
				oss << http_partial;
			}
			else {
				oss << http_ok;
			}
			oss << http_connection;
			oss << "Content-Type: video/mpeg\r\n";
			oss << http_server;
			oss << "Accept-Ranges: bytes\r\n";
			oss << "Content-Length: " << Util::ultostr(content_length) << "\r\n";
			oss << "Content-Range: bytes " <<
					Util::ultostr(seek_offset) << "-" <<
					Util::ultostr(source->stream_length - 1) << "/" <<
					Util::ultostr(source->stream_length) << "\r\n";
			oss << http_done;
		}
		break;
	case HttpHeader::TRANSCODING_LIVE: {
			oss << http_ok;
			oss << http_connection;
			oss << "Content-Type: video/mpeg\r\n";
			oss << http_server;
			oss << http_done;
		}
		break;
	case HttpHeader::M3U: {
			std::ostringstream m3u_oss;
			m3u_oss << "#EXTM3U\n";
			m3u_oss << "#EXTVLCOPT--http-reconnect=true\n";
			m3u_oss << "http://" << params["Host"] << "/file?file=" << page_params["file"];
			if (page_params["position"] != "") {
				m3u_oss << "&position=" << page_params["position"];
			}
			m3u_oss << "\n";
			m3u_oss << http_done;

			std::string m3u_content = m3u_oss.str();

			oss << http_partial;
			oss << "Content-Type: audio/x-mpegurl\r\n";
			oss << "Accept-Ranges: bytes\r\n";
			oss << http_connection;
			oss << http_server;
			oss << "Content-Length: " << Util::ultostr(m3u_content.length()) << "\r\n";
			oss << "Content-Range: bytes 0-" <<
					Util::ultostr(m3u_content.length() - 1) << "/" <<
					Util::ultostr(m3u_content.length()) << "\r\n";
			oss << http_done;
			oss << m3u_content;
		}
		break;
	default: return "";
	}
	return oss.str();
}
//----------------------------------------------------------------------

std::string HttpHeader::read_request()
{
	std::string request = "";
	while (true) {
		char buffer[128] = {0};
		fgets(buffer, 127, stdin);

		request += buffer;
		if(request.find("\r\n\r\n") != string::npos)
			break;
	}
	return request;
}
//----------------------------------------------------------------------

std::string HttpUtil::http_error(int errcode, std::string errmsg)
{
	std::ostringstream oss;

	oss << "HTTP/1.1 " << Util::ultostr(errcode) << " " << errmsg << "\r\n";
	oss << "Content-Type: text/html\r\n";
	oss << "Connection: close\r\n";
	oss << "Accept-Ranges: bytes\r\n";
	oss << "\r\n";

	return oss.str();
}
//----------------------------------------------------------------------