xbmc
IHTTPRequestHandler.h
1 /*
2  * Copyright (C) 2011-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "utils/HttpRangeUtils.h"
12 
13 #include <map>
14 #include <stdint.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <string>
19 
20 #include <microhttpd.h>
21 #include <sys/select.h>
22 #include <sys/socket.h>
23 #include <sys/types.h>
24 
25 #if MHD_VERSION >= 0x00097002
26 using MHD_RESULT = MHD_Result;
27 #else
28 using MHD_RESULT = int;
29 #endif
30 
31 class CDateTime;
32 class CWebServer;
33 
34 enum HTTPMethod
35 {
36  UNKNOWN,
37  POST,
38  GET,
39  HEAD
40 };
41 
42 HTTPMethod GetHTTPMethod(const char *method);
43 std::string GetHTTPMethod(HTTPMethod method);
44 
45 typedef enum HTTPResponseType
46 {
47  HTTPNone,
48  // creates and returns a HTTP error
49  HTTPError,
50  // creates and returns a HTTP redirect response
51  HTTPRedirect,
52  // creates a HTTP response with the content from a file
53  HTTPFileDownload,
54  // creates a HTTP response from a buffer without copying or freeing the buffer
55  HTTPMemoryDownloadNoFreeNoCopy,
56  // creates a HTTP response from a buffer by copying but not freeing the buffer
57  HTTPMemoryDownloadNoFreeCopy,
58  // creates a HTTP response from a buffer without copying followed by freeing the buffer
59  // the buffer must have been malloc'ed and not new'ed
60  HTTPMemoryDownloadFreeNoCopy,
61  // creates a HTTP response from a buffer by copying followed by freeing the buffer
62  // the buffer must have been malloc'ed and not new'ed
63  HTTPMemoryDownloadFreeCopy
64 } HTTPResponseType;
65 
66 typedef struct HTTPRequest
67 {
68  CWebServer *webserver;
69  struct MHD_Connection *connection;
70  std::string pathUrlFull;
71  std::string pathUrl;
72  HTTPMethod method;
73  std::string version;
74  CHttpRanges ranges;
75 } HTTPRequest;
76 
77 typedef struct HTTPResponseDetails {
78  HTTPResponseType type;
79  int status;
80  std::multimap<std::string, std::string> headers;
81  std::string contentType;
82  uint64_t totalLength;
84 
86 {
87 public:
88  virtual ~IHTTPRequestHandler() = default;
89 
100  virtual IHTTPRequestHandler* Create(const HTTPRequest &request) const = 0;
101 
108  virtual int GetPriority() const { return 0; }
109 
116  virtual bool CanHandleRequest(const HTTPRequest &request) const = 0;
117 
123  virtual MHD_RESULT HandleRequest() = 0;
124 
128  virtual bool CanHandleRanges() const { return false; }
129 
133  virtual bool CanBeCached() const { return false; }
134 
140  virtual int GetMaximumAgeForCaching() const { return 0; }
141 
147  virtual bool GetLastModifiedDate(CDateTime &lastModified) const { return false; }
148 
154  virtual HttpResponseRanges GetResponseData() const { return HttpResponseRanges(); }
155 
161  virtual std::string GetRedirectUrl() const { return ""; }
162 
168  virtual std::string GetResponseFile() const { return ""; }
169 
173  const HTTPRequest& GetRequest() const { return m_request; }
174 
178  bool IsRequestRanged() const { return m_ranged; }
179 
183  void SetRequestRanged(bool ranged) { m_ranged = ranged; }
184 
190  void SetResponseStatus(int status) { m_response.status = status; }
191 
198  bool HasResponseHeader(const std::string &field) const;
199 
208  bool AddResponseHeader(const std::string &field, const std::string &value, bool allowMultiple = false);
209 
213  const HTTPResponseDetails& GetResponseDetails() const { return m_response; }
214 
221  void AddPostField(const std::string &key, const std::string &value);
228  bool AddPostData(const char *data, size_t size);
229 
230 protected:
232  explicit IHTTPRequestHandler(const HTTPRequest &request);
233 
234  virtual bool appendPostData(const char *data, size_t size)
235  { return true; }
236 
237  bool GetRequestedRanges(uint64_t totalLength);
238  bool GetHostnameAndPort(std::string& hostname, uint16_t &port);
239 
240  HTTPRequest m_request;
241  HTTPResponseDetails m_response;
242 
243  std::map<std::string, std::string> m_postFields;
244 
245 private:
246  bool m_ranged = false;
247 };
void SetRequestRanged(bool ranged)
Sets whether the HTTP request contains ranges or not.
Definition: IHTTPRequestHandler.h:183
Definition: IHTTPRequestHandler.h:66
virtual bool GetLastModifiedDate(CDateTime &lastModified) const
Returns the last modification date of the response data.
Definition: IHTTPRequestHandler.h:147
Definition: WebServer.h:25
virtual int GetPriority() const
Returns the priority of the HTTP request handler.
Definition: IHTTPRequestHandler.h:108
virtual std::string GetRedirectUrl() const
Returns the URL to which the request should be redirected.
Definition: IHTTPRequestHandler.h:161
virtual int GetMaximumAgeForCaching() const
Returns the maximum age (in seconds) for which the response can be cached.
Definition: IHTTPRequestHandler.h:140
const HTTPResponseDetails & GetResponseDetails() const
Returns the HTTP response header details.
Definition: IHTTPRequestHandler.h:213
virtual std::string GetResponseFile() const
Returns the path to the file that should be returned as the response.
Definition: IHTTPRequestHandler.h:168
Definition: IHTTPRequestHandler.h:77
Definition: HttpRangeUtils.h:68
const HTTPRequest & GetRequest() const
Returns the HTTP request handled by the HTTP request handler.
Definition: IHTTPRequestHandler.h:173
DateTime class, which uses FileTime as it&#39;s base.
Definition: XBDateTime.h:63
virtual bool CanBeCached() const
Whether the HTTP response can be cached.
Definition: IHTTPRequestHandler.h:133
virtual bool CanHandleRanges() const
Whether the HTTP response could also be provided in ranges.
Definition: IHTTPRequestHandler.h:128
bool IsRequestRanged() const
Returns true if the HTTP request is ranged, otherwise false.
Definition: IHTTPRequestHandler.h:178
virtual HttpResponseRanges GetResponseData() const
Returns the ranges with raw data belonging to the response.
Definition: IHTTPRequestHandler.h:154
void SetResponseStatus(int status)
Sets the response status of the HTTP response.
Definition: IHTTPRequestHandler.h:190
Definition: IHTTPRequestHandler.h:85