Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
url_utils.cpp
Go to the documentation of this file.
1
17#include "http/url_utils.h"
18#include <sstream>
19#include <lwip/sockets.h>
20#include <lwip/ip_addr.h>
21#include <lwip/memp.h>
22#include <string>
23#include <algorithm>
24#include <cctype>
25#include "network/Tcp.h"
26
28std::string urlDecode(const std::string &src)
29{
30 std::string ret;
31 for (size_t i = 0; i < src.length(); ++i)
32 {
33 if (src[i] == '%')
34 {
35 if (i + 2 < src.length())
36 {
37 std::istringstream iss(src.substr(i + 1, 2));
38 int hexVal;
39 if (iss >> std::hex >> hexVal)
40 {
41 ret += static_cast<char>(hexVal);
42 i += 2;
43 }
44 else
45 {
46 ret += '%';
47 }
48 }
49 else
50 {
51 ret += '%';
52 }
53 }
54 else if (src[i] == '+')
55 {
56 ret += ' ';
57 }
58 else
59 {
60 ret += src[i];
61 }
62 }
63 return ret;
64}
65
67std::unordered_multimap<std::string, std::string> parseUrlEncoded(const std::string& data)
68{
69 std::unordered_multimap<std::string, std::string> params;
70
71 std::istringstream stream(data);
72 std::string pair;
73 while (std::getline(stream, pair, '&'))
74 {
75 size_t pos = pair.find('=');
76 if (pos != std::string::npos)
77 {
78 std::string key = pair.substr(0, pos);
79 std::string value = pair.substr(pos + 1);
80 // Decode both key and value
81 params.emplace(urlDecode(key), urlDecode(value));
82 }
83 }
84 return params;
85}
86
88std::string getClientIpFromTcp(Tcp* tcp)
89{
90 sockaddr_in addr;
91 socklen_t len = sizeof(addr);
92
93 int sock = tcp->getSocketFd(); // Add this method if needed
94 if (lwip_getpeername(sock, (sockaddr *)&addr, &len) == 0)
95 {
96 ip_addr_t ip;
97 ip.addr = addr.sin_addr.s_addr;
98 return std::string(ipaddr_ntoa(&ip));
99 }
100 return "0.0.0.0";
101}
102
103
105std::string getMimeType(const std::string &filePath)
106{
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"}};
124
125 size_t extPos = filePath.find_last_of(".");
126 if (extPos != std::string::npos)
127 {
128 std::string ext = filePath.substr(extPos); // Extract extension
129 auto it = mimeTypes.find(ext);
130 if (it != mimeTypes.end())
131 {
132 return it->second; // Return corresponding MIME type
133 }
134 }
135
136 return "application/octet-stream"; // Default MIME type
137}
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).
Definition Tcp.h:39
int getSocketFd() const
Get the raw socket file descriptor (may be -1 for TLS-only connection).
Definition Tcp.h:124
std::string getMimeType(const std::string &filePath)
std::string urlDecode(const std::string &src)
Definition url_utils.cpp:28
std::string getClientIpFromTcp(Tcp *tcp)
Get the client IP address from a socket.
Definition url_utils.cpp:88
std::unordered_multimap< std::string, std::string > parseUrlEncoded(const std::string &data)
Definition url_utils.cpp:67
Utility functions for URL decoding, form parsing, MIME type lookup, and IP address extraction.