17#include <openssl/err.h>
18#include <openssl/ssl.h>
21#define PHOTON_HTTP_VER 1.1
23#define PHOTON_USE_HTTPS (443)
24#define PHOTON_USE_HTTP (80)
25#define PHOTON_HTTP_AGENT "Photon/1.0.0 (Photon) Photon/1.0.0 Version/1.0.0"
56 return {.
t_name =
"N/A", .t_mime =
"*/*"};
58 std::string extension = strchr(
name,
'.');
60 if (strstr(extension.c_str(),
".png"))
61 return {.t_name =
"PNG Image", .t_mime =
"image/png"};
62 else if (strstr(extension.c_str(),
".html"))
63 return {.t_name =
"HTML Document", .t_mime =
"text/html"};
64 else if (strstr(extension.c_str(),
".bmp"))
65 return {.t_name =
"BMP Image", .t_mime =
"image/bmp"};
66 else if (strstr(extension.c_str(),
".webp"))
67 return {.t_name =
"WEBP Image", .t_mime =
"image/webp"};
68 else if (strstr(extension.c_str(),
".exe"))
69 return {.t_name =
"Microsoft Portable Executable", .t_mime =
"application/vnd.microsoft.executable"};
70 else if (strstr(extension.c_str(),
".exec"))
71 return {.t_name =
"NeKernel Preferred Executable Format", .t_mime =
"application/vnd.ne.executable"};
72 else if (strstr(extension.c_str(),
".jpg"))
73 return {.t_name =
"JPEG Image", .t_mime =
"image/jpeg"};
74 else if (strstr(extension.c_str(),
".zip"))
75 return {.t_name =
"PKZIP Archive", .t_mime =
"application/zip"};
76 else if (strstr(extension.c_str(),
".svg"))
77 return {.t_name =
"SVG Document", .t_mime =
"image/svg+xml"};
79 return {.t_name =
"N/A", .t_mime =
"*/y"};
135 explicit HTTPError(
const std::uint16_t what) :
std::runtime_error(
"HTTP_RESULT_")
151 std::string
base = this->what();
170 const std::string host,
171 const std::string request_type,
172 const size_t length = 0,
173 std::vector<std::pair<std::string, std::string>> headers = {},
176 if (path.empty() || host.empty())
181 std::string request = request_type;
185 request +=
" HTTP/1.1\r\n";
189 request +=
"User-Agent: ";
194 auto mime_struct = factory(
const_cast<char*
>(path.data()));
196 if (length > 0 && request_type != PHOTON_HTTP_GET)
198 request +=
"Content-Length: ";
199 request += std::to_string(length);
203 request +=
"Accept: ";
204 request += mime_struct.t_mime;
207 for (
const auto&
header : headers)
215 request +=
"Connection: keep-alive\r\n";
218 if (
data.size() > 0 && request_type == PHOTON_HTTP_POST)
236 auto value = http.substr(http.find(
name));
244 static bool has_field(
const std::string& http,
const std::string& rest)
252 return http.find(rest) != std::string::npos;
257 String value = IHTTPHelper::get_field_value(http,
"Content-Length:");
260 return IHTTPHelper::bad_pos;
264 for (
size_t first = 0; first <
value.size(); ++first)
266 if (
value[first] ==
' ')
269 if (
value[first] >=
'0' &&
value[first] <=
'9')
271 final +=
value[first];
277 return std::atol(
final.c_str());
280 static const int bad_pos = 0;
297 if (
m_Ssl ==
nullptr)
299 fprintf(stderr,
"SSL_new() failed\n");
301 throw BrowserError(
"Bad SSL context, SSL_new() failed!");
333 memset(&sock->m_Addr, 0,
sizeof(
struct sockaddr_in));
335 sock->m_Addr.sin_family = AF_INET;
337 sock->m_Addr.sin_addr.s_addr = inet_addr(dns.c_str());
342 if (sock->m_Addr.sin_addr.s_addr == INADDR_NONE)
344 struct hostent* host = gethostbyname(dns.c_str());
350 PHOTON_INFO(
"Invalid hostname! returning nullptr...");
356 sock->m_Addr.sin_addr.s_addr = *((u_long*)host->h_addr);
359 sock->m_Dns = std::string{dns.data()};
362 ::connect(sock->m_Socket,
reinterpret_cast<struct sockaddr*
>(&sock->m_Addr),
sizeof(sock->m_Addr));
372 SSL_set_fd(
m_Ssl, sock->m_Socket);
373 auto status = SSL_connect(
m_Ssl);
380 PHOTON_INFO(std::string(
"Connected with HTTPS encryption: ") + SSL_get_cipher(
m_Ssl));
413 if (!sock || !len || !
bytes)
422 return ::send(sock->m_Socket,
bytes, len, 0) > 0;
428 if (!sock || !
bytes || len < 1)
444 auto ret = ::recv(sock->m_Socket,
bytes, len, 0);
457 const SSL_METHOD* method = TLS_client_method();
458 SSL_CTX* ctx = SSL_CTX_new(method);
462 ERR_print_errors_fp(stderr);
#define PHOTON_WARN(...)
Definition Core.hpp:167
#define PHOTON_INFO(...)
Definition Core.hpp:166
#define PHOTON_USE_HTTPS
Definition IHTTPProtocol.hpp:23
#define PHOTON_HTTP_AGENT
Definition IHTTPProtocol.hpp:25
#define PHOTON_CLOSE
Definition Socket.hpp:38
#define PHOTON_SHUTDOWN
Definition Socket.hpp:39
#define SOCKET_ERROR
Definition Socket.hpp:27
#define PHOTON_SOCKET
Definition Socket.hpp:37
Definition IHTTPProtocol.hpp:86
HTTPSocket(const HTTPSocket &)=default
std::string m_Dns
Definition IHTTPProtocol.hpp:92
HTTPSocket & operator=(const HTTPSocket &)=default
friend HTTPWriter
Definition IHTTPProtocol.hpp:95
Network::CSocket m_Socket
Definition IHTTPProtocol.hpp:93
Definition IHTTPProtocol.hpp:133
HTTPError(const std::uint16_t what)
Definition IHTTPProtocol.hpp:135
HTTPError(const HTTPError &)=default
std::string as_string() noexcept
Definition IHTTPProtocol.hpp:149
int mError
Definition IHTTPProtocol.hpp:158
HTTPError & operator=(const HTTPError &)=default
~HTTPError() override=default
Int32 error() const
Definition IHTTPProtocol.hpp:144
Definition IHTTPProtocol.hpp:286
SSL * m_Ssl
Definition IHTTPProtocol.hpp:472
bool send_from_socket(HTTPSharedPtr &sock, const char *bytes, size_t len)
Definition IHTTPProtocol.hpp:411
HTTPSharedPtr m_Socket
Definition IHTTPProtocol.hpp:470
SSL_CTX * _init_ssl(void) noexcept
Definition IHTTPProtocol.hpp:455
HTTPWriter & operator=(const HTTPWriter &)=default
SSL_CTX * m_SslCtx
Definition IHTTPProtocol.hpp:471
bool close_socket() noexcept
Definition IHTTPProtocol.hpp:392
HTTPWriter(const HTTPWriter &)=default
int64_t read_from_socket(HTTPSharedPtr &sock, char *bytes, int len)
Definition IHTTPProtocol.hpp:426
HTTPSharedPtr create_and_connect(const std::string dns)
Definition IHTTPProtocol.hpp:314
HTTPWriter(bool use_https)
Definition IHTTPProtocol.hpp:288
Definition IHTTPProtocol.hpp:167
static bool has_field(const std::string &http, const std::string &rest)
Definition IHTTPProtocol.hpp:244
static std::string form_request(const std::string path, const std::string host, const std::string request_type, const size_t length=0, std::vector< std::pair< std::string, std::string > > headers={}, String data="")
Definition IHTTPProtocol.hpp:169
static String get_field_value(const std::string &http, const std::string &name)
Definition IHTTPProtocol.hpp:228
static long content_length(const std::string &http)
Definition IHTTPProtocol.hpp:255
Definition IHTTPProtocol.hpp:37
MIMEFactory & operator=(const MIMEFactory &)=default
MIMEFactory(const MIMEFactory &)=default
MIMEFactory::MIME operator()(char *name)
Definition IHTTPProtocol.hpp:53
#define SD_BOTH
Definition Config.hpp:40
#define INVALID_SOCKET
Definition Config.hpp:44
#define PHOTON_API
Definition Config.hpp:64
RequestType
Definition IHTTPProtocol.hpp:106
Definition IHTTPProtocol.hpp:28
int16_t PHOTON_HTTP_PORT
Definition IHTTPProtocol.hpp:29
std::string PHOTON_HTTP_DELETE
Definition IHTTPProtocol.hpp:164
std::shared_ptr< HTTP::HTTPSocket > HTTPSharedPtr
Definition IHTTPProtocol.hpp:283
std::string PHOTON_HTTP_GET
Definition IHTTPProtocol.hpp:161
std::string PHOTON_HTTP_POST
Definition IHTTPProtocol.hpp:162
HTTP_ERROR_LIST
Definition IHTTPProtocol.hpp:122
@ HTTP_NOT_FOUND
Definition IHTTPProtocol.hpp:125
@ HTTP_ERROR_COUNT
Definition IHTTPProtocol.hpp:129
@ HTTP_DNS_ERROR
Definition IHTTPProtocol.hpp:127
@ HTTP_BAD_GATEWAY
Definition IHTTPProtocol.hpp:124
@ HTTP_BAD_REQ
Definition IHTTPProtocol.hpp:126
@ HTTP_OK
Definition IHTTPProtocol.hpp:123
@ HTTP_INTERNAL_ERROR
Definition IHTTPProtocol.hpp:128
std::string PHOTON_HTTP_PUT
Definition IHTTPProtocol.hpp:163
uintptr_t CSocket
Definition Socket.hpp:67
std::string String
Definition Core.hpp:37
std::int32_t Int32
Definition Config.hpp:95
auto fprintf(std::FILE *f, const S &fmt, const T &... args) -> int
Definition printf.h:760
Definition IHTTPProtocol.hpp:88
Definition IHTTPProtocol.hpp:40
std::string t_mime
Definition IHTTPProtocol.hpp:42
std::string t_name
Definition IHTTPProtocol.hpp:41
base
Definition tag_strings.h:7
header
Definition tag_strings.h:26