19#include <lwip/sockets.h>
20#include <lwip/ip_addr.h>
31 for (
size_t i = 0; i < src.length(); ++i)
35 if (i + 2 < src.length())
37 std::istringstream iss(src.substr(i + 1, 2));
39 if (iss >> std::hex >> hexVal)
41 ret +=
static_cast<char>(hexVal);
54 else if (src[i] ==
'+')
67std::unordered_multimap<std::string, std::string>
parseUrlEncoded(
const std::string& data)
69 std::unordered_multimap<std::string, std::string> params;
71 std::istringstream stream(data);
73 while (std::getline(stream, pair,
'&'))
75 size_t pos = pair.find(
'=');
76 if (pos != std::string::npos)
78 std::string key = pair.substr(0, pos);
79 std::string value = pair.substr(pos + 1);
91 socklen_t len =
sizeof(addr);
94 if (lwip_getpeername(sock, (sockaddr *)&addr, &len) == 0)
97 ip.addr = addr.sin_addr.s_addr;
98 return std::string(ipaddr_ntoa(&ip));
107 static const std::unordered_map<std::string, std::string> mimeTypes = {
108 {
".html",
"text/html"},
109 {
".css",
"text/css"},
110 {
".js",
"application/javascript"},
111 {
".json",
"application/json"},
112 {
".jpg",
"image/jpeg"},
113 {
".jpeg",
"image/jpeg"},
114 {
".png",
"image/png"},
115 {
".gif",
"image/gif"},
116 {
".txt",
"text/plain"},
117 {
".xml",
"application/xml"},
118 {
".pdf",
"application/pdf"},
119 {
".zip",
"application/zip"},
120 {
".mp4",
"video/mp4"},
121 {
".mp3",
"audio/mpeg"},
122 {
".wav",
"audio/wav"},
123 {
".csv",
"text/csv"}};
125 size_t extPos = filePath.find_last_of(
".");
126 if (extPos != std::string::npos)
128 std::string ext = filePath.substr(extPos);
129 auto it = mimeTypes.find(ext);
130 if (it != mimeTypes.end())
136 return "application/octet-stream";
General-purpose TCP socket abstraction with optional TLS support for both client and server use.
General-purpose TCP socket wrapper with optional TLS support via mbedTLS (altcp).
int getSocketFd() const
Get the raw socket file descriptor (may be -1 for TLS-only connection).
std::string getMimeType(const std::string &filePath)
std::string urlDecode(const std::string &src)
std::string getClientIpFromTcp(Tcp *tcp)
Get the client IP address from a socket.
std::unordered_multimap< std::string, std::string > parseUrlEncoded(const std::string &data)
Utility functions for URL decoding, form parsing, MIME type lookup, and IP address extraction.