2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
11 #include "network/httprequesthandler/IHTTPRequestHandler.h"
12 #include "threads/CriticalSection.h"
13 #include "utils/logtypes.h"
29 virtual ~CWebServer() = default;
31 bool Start(uint16_t port
, const std::string
&username
, const std::string
&password
);
34 static bool WebServerSupportsSSL();
35 void SetCredentials(const std::string
&username
, const std::string
&password
);
37 void RegisterRequestHandler(IHTTPRequestHandler
*handler
);
38 void UnregisterRequestHandler(IHTTPRequestHandler
*handler
);
41 typedef struct ConnectionHandler
45 std::shared_ptr
<IHTTPRequestHandler
> requestHandler
;
46 struct MHD_PostProcessor
* postprocessor
= nullptr;
47 int errorStatus
= MHD_HTTP_OK
;
49 explicit ConnectionHandler(const std::string
& uri
) : fullUri(uri
), requestHandler(nullptr) {}
52 virtual void LogRequest(const char* uri
) const;
54 virtual MHD_RESULT
HandlePartialRequest(struct MHD_Connection
*connection
, ConnectionHandler
* connectionHandler
, const HTTPRequest
& request
,
55 const char *upload_data
, size_t *upload_data_size
, void **con_cls
);
56 virtual MHD_RESULT
HandleRequest(const std::shared_ptr
<IHTTPRequestHandler
>& handler
);
57 virtual MHD_RESULT
FinalizeRequest(const std::shared_ptr
<IHTTPRequestHandler
>& handler
, int responseStatus
, struct MHD_Response
*response
);
60 struct MHD_Daemon
* StartMHD(unsigned int flags
, int port
);
62 std::shared_ptr
<IHTTPRequestHandler
> FindRequestHandler(const HTTPRequest
& request
) const;
64 MHD_RESULT
AskForAuthentication(const HTTPRequest
& request
) const;
65 bool IsAuthenticated(const HTTPRequest
& request
) const;
67 bool IsRequestCacheable(const HTTPRequest
& request
) const;
68 bool IsRequestRanged(const HTTPRequest
& request
, const CDateTime
&lastModified
) const;
70 void SetupPostDataProcessing(const HTTPRequest
& request
, ConnectionHandler
*connectionHandler
, std::shared_ptr
<IHTTPRequestHandler
> handler
, void **con_cls
) const;
71 bool ProcessPostData(const HTTPRequest
& request
, ConnectionHandler
*connectionHandler
, const char *upload_data
, size_t *upload_data_size
, void **con_cls
) const;
72 void FinalizePostDataProcessing(ConnectionHandler
*connectionHandler
) const;
74 MHD_RESULT
CreateMemoryDownloadResponse(const std::shared_ptr
<IHTTPRequestHandler
>& handler
, struct MHD_Response
*&response
) const;
75 MHD_RESULT
CreateRangedMemoryDownloadResponse(const std::shared_ptr
<IHTTPRequestHandler
>& handler
, struct MHD_Response
*&response
) const;
77 MHD_RESULT
CreateRedirect(struct MHD_Connection
*connection
, const std::string
&strURL
, struct MHD_Response
*&response
) const;
78 MHD_RESULT
CreateFileDownloadResponse(const std::shared_ptr
<IHTTPRequestHandler
>& handler
, struct MHD_Response
*&response
) const;
79 MHD_RESULT
CreateErrorResponse(struct MHD_Connection
*connection
, int responseType
, HTTPMethod method
, struct MHD_Response
*&response
) const;
80 MHD_RESULT
CreateMemoryDownloadResponse(struct MHD_Connection
*connection
, const void *data
, size_t size
, bool free
, bool copy
, struct MHD_Response
*&response
) const;
82 MHD_RESULT
SendResponse(const HTTPRequest
& request
, int responseStatus
, MHD_Response
*response
) const;
83 MHD_RESULT
SendErrorResponse(const HTTPRequest
& request
, int errorType
, HTTPMethod method
) const;
85 MHD_RESULT
AddHeader(struct MHD_Response
*response
, const std::string
&name
, const std::string
&value
) const;
87 void LogRequest(const HTTPRequest
& request
) const;
88 void LogResponse(const HTTPRequest
& request
, int responseStatus
) const;
90 static std::string
CreateMimeTypeFromExtension(const char *ext
);
92 // MHD callback implementations
93 static void* UriRequestLogger(void *cls
, const char *uri
);
95 static ssize_t
ContentReaderCallback (void *cls
, uint64_t pos
, char *buf
, size_t max
);
96 static void ContentReaderFreeCallback(void *cls
);
98 static MHD_RESULT
AnswerToConnection (void *cls
, struct MHD_Connection
*connection
,
99 const char *url
, const char *method
,
100 const char *version
, const char *upload_data
,
101 size_t *upload_data_size
, void **con_cls
);
102 static MHD_RESULT
HandlePostField(void *cls
, enum MHD_ValueKind kind
, const char *key
,
103 const char *filename
, const char *content_type
,
104 const char *transfer_encoding
, const char *data
, uint64_t off
,
107 bool LoadCert(std::string
&skey
, std::string
&scert
);
109 static Logger
GetLogger();
112 struct MHD_Daemon
*m_daemon_ip6
= nullptr;
113 struct MHD_Daemon
*m_daemon_ip4
= nullptr;
114 bool m_running
= false;
115 size_t m_thread_stacksize
= 0;
116 bool m_authenticationRequired
= false;
117 std::string m_authenticationUsername
;
118 std::string m_authenticationPassword
;
121 mutable CCriticalSection m_critSection
;
122 std::vector
<IHTTPRequestHandler
*> m_requestHandlers
;