Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
url_utils.h File Reference

Utility functions for URL decoding, form parsing, MIME type lookup, and IP address extraction. More...

#include <string>
#include <unordered_map>
#include <sstream>
+ Include dependency graph for url_utils.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

std::string trim (const std::string &s)
 Trim whitespace from the beginning and end of a string.
 
std::string urlDecode (const std::string &src)
 Decode a URL-encoded string (e.g., "a%20b+c" -> "a b c").
 
std::unordered_multimap< std::string, std::string > parseUrlEncoded (const std::string &data)
 Parse a URL-encoded key-value string into a map.
 
std::string getClientIpFromSocket (int sock)
 Get the client IP address from a socket.
 
std::string getMimeType (const std::string &filePath)
 Get the MIME type based on a file path's extension.
 

Detailed Description

These functions support core HTTP functionality including decoding URL-encoded strings, parsing application/x-www-form-urlencoded bodies, detecting client IP addresses, and determining appropriate MIME types for static file serving.

Version
0.1
Date
2025-03-31
Author
...

Definition in file url_utils.h.

Function Documentation

◆ getClientIpFromSocket()

std::string getClientIpFromSocket ( int  sock)

Uses lwIP's lwip_getpeername() to extract the IP address of the remote peer.

Parameters
sockThe socket file descriptor.
Returns
The client IP address as a string. Returns "0.0.0.0" on failure.

◆ getMimeType()

std::string getMimeType ( const std::string &  filePath)

Used when serving static files to determine the appropriate Content-Type.

Parameters
filePathThe file path (e.g., "/index.html").
Returns
A MIME type string (e.g., "text/html").

Get the MIME type based on a file path's extension.

Get the MIME type based on a file path's extension.

Definition at line 105 of file url_utils.cpp.

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}

Referenced by FileHandler::serveFile().

+ Here is the caller graph for this function:

◆ parseUrlEncoded()

std::unordered_multimap< std::string, std::string > parseUrlEncoded ( const std::string &  data)

Converts data in the format "key1=value1&key2=value2" into a map. Both keys and values are URL-decoded.

Parameters
dataURL-encoded form data.
Returns
A map of key-value pairs.

Parse a URL-encoded key-value string into a map.

Parse a URL-encoded key-value string into a map.

Definition at line 67 of file url_utils.cpp.

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}
std::string urlDecode(const std::string &src)
Definition url_utils.cpp:28

References urlDecode().

Referenced by HttpRequest::getFormParams(), and HttpRequest::getQueryParams().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ trim()

std::string trim ( const std::string &  s)
inline
Parameters
sThe input string.
Returns
A new string with leading/trailing whitespace removed.

Definition at line 27 of file url_utils.h.

27 {
28 size_t start = s.find_first_not_of(" \t\r\n");
29 size_t end = s.find_last_not_of(" \t\r\n");
30 return (start == std::string::npos) ? "" : s.substr(start, end - start + 1);
31 }

◆ urlDecode()

std::string urlDecode ( const std::string &  src)

Converts percent-encoded characters and replaces '+' with space.

Parameters
srcThe URL-encoded input string.
Returns
The decoded string.

Decode a URL-encoded string (e.g., "a%20b+c" -> "a b c").

Decode a URL-encoded string (e.g., "a%20b+c" -> "a b c").

Definition at line 28 of file url_utils.cpp.

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}

Referenced by HttpFileserver::handle_static_request(), Router::handleRequest(), and parseUrlEncoded().

+ Here is the caller graph for this function: