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

Implementation of URL utility functions for decoding, parsing, and MIME type detection. More...

#include "http/url_utils.h"
#include <sstream>
#include <lwip/sockets.h>
#include <lwip/ip_addr.h>
#include <lwip/memp.h>
#include <string>
#include <algorithm>
#include <cctype>
#include "network/Tcp.h"
+ Include dependency graph for url_utils.cpp:

Go to the source code of this file.

Functions

std::string urlDecode (const std::string &src)
 
std::unordered_multimap< std::string, std::string > parseUrlEncoded (const std::string &data)
 
std::string getClientIpFromTcp (Tcp *tcp)
 Get the client IP address from a socket.
 
std::string getMimeType (const std::string &filePath)
 

Detailed Description

Author
Ian Archbell

Part of the PicoFramework HTTP server. This module provides functions for URL decoding, parsing URL-encoded form data, extracting client IP addresses from sockets, and determining MIME types based on file extensions.

Version
0.1
Date
2025-03-31
License:\n MIT License

Definition in file url_utils.cpp.

Function Documentation

◆ getClientIpFromTcp()

std::string getClientIpFromTcp ( Tcp tcp)

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.

Definition at line 88 of file url_utils.cpp.

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}
int getSocketFd() const
Get the raw socket file descriptor (may be -1 for TLS-only connection).
Definition Tcp.h:124

References Tcp::getSocketFd().

+ Here is the call graph for this function:

◆ getMimeType()

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

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)

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:

◆ urlDecode()

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

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: