summaryrefslogtreecommitdiff
path: root/PlexHTTPRequestHandler.cpp
blob: d6c8ad339a5e45489f09c9ca8cc5a5974e1036f3 (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
#include "PlexHTTPRequestHandler.h"
#include <vdr/remote.h>
#include <vdr/keys.h>
#include <unistd.h>

namespace plexclient
{

void PlexHTTPRequestHandler::AddHeaders(Poco::Net::HTTPServerResponse& response, Poco::Net::HTTPServerRequest& request) {
	if(request.getMethod() == Poco::Net::HTTPRequest::HTTP_OPTIONS) {
		response.setContentType("text/plain");	

		response.add("X-Plex-Client-Identifier", Config::GetInstance().GetUUID());
		response.add("Connection", "Close");
		response.add("Access-Control-Max-Age", "1209600");
		response.add("Access-Control-Allow-Origin", "*");
		response.add("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT, HEAD");
		response.add("Access-Control-Allow-Headers", "x-plex-version, x-plex-platform-version, "
													 "x-plex-username, x-plex-client-identifier, "
													 "x-plex-target-client-identifier, x-plex-device-name, "
													 "x-plex-platform, x-plex-product, accept, x-plex-device");
	} else {
		response.setContentType("application/x-www-form-urlencoded");	
		
		response.add("Access-Control-Allow-Origin", "*");
		response.add("X-Plex-Version", VERSION);
		response.add("X-Plex-Client-Identifier", Config::GetInstance().GetUUID());
		response.add("X-Plex-Product", "player");
		response.add("X-Plex-Product", "PlexVDR");
		response.add("X-Plex-Device-Name", Config::GetInstance().GetHostname());
		response.add("X-Plex-Platform", "VDR");
		response.add("X-Plex-Model", "Linux");
		response.add("X-Plex-Device", "PC");
		response.add("X-Plex-Username", Config::GetInstance().GetUsername());
	}
	//header.MessageHeader(header);
}

std::map<std::string, std::string> PlexHTTPRequestHandler::ParseQuery(std::string query) {
	std::map<std::string, std::string> querymap;
	Poco::StringTokenizer queryTokens(query, "&");
	for(Poco::StringTokenizer::Iterator token = queryTokens.begin() ; token != queryTokens.end(); ++token) {
		Poco::StringTokenizer subTokens(*token, "=");
		querymap[subTokens[0]] = subTokens[1];
	}
	return querymap;
}

std::string PlexHTTPRequestHandler::GetOKMsg() {
	return "<?xml version=\"1.0\" encoding=\"utf-8\"?> <Response code=\"200\" status=\"OK\" />";
}

void PlexHTTPRequestHandler::handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response){	
	AddHeaders(response, request);
	std::ostream& ostr = response.send();
	ostr << GetOKMsg();
}

void SubscribeRequestHandler::handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response){
	// parse query
	Poco::URI uri(request.getURI());
	std::map<std::string, std::string> query = ParseQuery(uri.getQuery()); // port=32400&commandID=0&protocol=http
	std::string path = uri.getPath(); // /player/timeline/subscribe
	
	std::string uuid = request.get("X-Plex-Client-Identifier");
	int port = atoi(query["port"].c_str());
	int command = atoi(query["commandID"].c_str());
	SubscriptionManager::GetInstance().AddSubscriber(Subscriber(query["protocol"], request.getHost(), port, uuid, command));
	
	AddHeaders(response, request);
	
	std::ostream& ostr = response.send();
	ostr << GetOKMsg();
	response.setStatus(Poco::Net::HTTPResponse::HTTP_REASON_OK);
}

void ResourceRequestHandler::handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response){
	AddHeaders(response, request);
	
	std::ostream& ostr = response.send();
	ostr << "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
			"<MediaContainer>"
			"<Player title=\"" << Config::GetInstance().GetHostname() << "\""
			" protocol=\"plex\""
			" protocolVersion=\"1\""
			" protocolCapabilities=\"navigation,playback,timeline\""
			" machineIdentifier=\"" << Config::GetInstance().GetUUID() << "\""
			" product=\"PlexVDR\""
			" platform=\"Linux\""
			" platformVersion=\"" << VERSION << "\""
			" deviceClass=\"HTPC\""
			"/> </MediaContainer>";
	
	response.setStatus(Poco::Net::HTTPResponse::HTTP_REASON_OK);

	dsyslog("[plex]Resources Response sent...");
}

void PlayerRequestHandler::handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response){
	Poco::URI uri(request.getURI());
	//Poco::StringTokenizer pathTokens(uri, "/");
	std::map<std::string, std::string> query = ParseQuery(uri.getQuery());
	
	if(query.find("wait") != query.end() && atoi(query["wait"].c_str()) == 1) {
		usleep(900 * 1000);
	}
	
	if(request.getMethod() == Poco::Net::HTTPRequest::HTTP_OPTIONS)  {
		AddHeaders(response, request);
		//response.setStatus(Poco::Net::HTTPResponse::HTTP_REASON_OK);
		std::ostream& ostr = response.send(); // Stream must not be empty!
		ostr << " ";
		response.setStatus(Poco::Net::HTTPResponse::HTTP_REASON_OK);
		dsyslog("[plex]OPTION Reply send");
		return;
	} 
	
	if(request.getURI().find("/poll")!= std::string::npos) {
		response.add("X-Plex-Client-Identifier",Config::GetInstance().GetUUID());
		response.add("Access-Control-Expose-Headers","X-Plex-Client-Identifier");
		response.add("Access-Control-Allow-Origin","*");
		response.setContentType("text/xml");
		
		std::ostream& ostr = response.send();
		ostr << SubscriptionManager::GetInstance().GetMsg(query["commandID"]);
		response.setStatus(Poco::Net::HTTPResponse::HTTP_REASON_OK);
		
	} 
	else if(request.getURI().find("/playback/playMedia")!= std::string::npos) {
		//std::cout << "playMedia_1" << std::endl;
		AddHeaders(response, request);
				
		//std::cout << "playMedia_2" << std::endl;
		std::string protocol = query["protocol"];
		std::string address = query["address"];
		std::string port = query["port"];
		std::string key = query["key"];
		//std::cout << "playMedia_3" << std::endl;
		
		std::string fullUrl = protocol + "://" + address + ":" + port + key; // Metainfo
		std::cout << "FullUrl: " << fullUrl << std::endl;
		
		MediaContainer *pCont = Plexservice::GetMediaContainer(fullUrl);
		std::string filePath = pCont->m_vVideos[0].m_Media.m_sPartKey;
		Poco::URI fileuri(fullUrl);
		fileuri.setPath(filePath);
		
		// MUSS im Maintread des Plugins/VDR gestartet werden
		ActionManager::GetInstance().AddAction(fileuri.toString());
		
		std::ostream& ostr = response.send();
		ostr << GetOKMsg();
		response.setStatus(Poco::Net::HTTPResponse::HTTP_REASON_OK);
		SubscriptionManager::GetInstance().Notify();
	}
	else if(request.getURI().find("/navigation")!= std::string::npos) {
		if(request.getURI().find("/moveUp")!= std::string::npos) {
			cRemote::Put(kUp);
		}
		else if(request.getURI().find("/moveDown")!= std::string::npos) {
			cRemote::Put(kDown);
		}
		else if(request.getURI().find("/moveLeft")!= std::string::npos) {
			cRemote::Put(kLeft);
		}
		else if(request.getURI().find("/moveRight")!= std::string::npos) {
			cRemote::Put(kRight);
		}
		else if(request.getURI().find("/select")!= std::string::npos) {
			cRemote::Put(kOk);
		}
		else if(request.getURI().find("/home")!= std::string::npos) {
			cRemote::Put(kMenu);
		}
		else if(request.getURI().find("/back")!= std::string::npos) {
			cRemote::Put(kBack);
		}
	}
	
}

}