mirror of
https://github.com/nmap/nmap.git
synced 2025-12-17 05:09:00 +00:00
As long as we're converting file names to URLs we may as well do it right and
do percent-encoding to avoid interpretation of characters such as ? and #.
This commit is contained in:
11
NmapOps.cc
11
NmapOps.cc
@@ -178,6 +178,7 @@ int NmapOps::TimeSinceStartMS(struct timeval *now) {
|
|||||||
// Convert a filename to a file:// URL. The return value must be freed.
|
// Convert a filename to a file:// URL. The return value must be freed.
|
||||||
char *filename_to_url(const char *filename) {
|
char *filename_to_url(const char *filename) {
|
||||||
std::string url(filename);
|
std::string url(filename);
|
||||||
|
char percent_buffer[10];
|
||||||
|
|
||||||
#if WIN32
|
#if WIN32
|
||||||
for (std::string::iterator p = url.begin(); p != url.end(); p++) {
|
for (std::string::iterator p = url.begin(); p != url.end(); p++) {
|
||||||
@@ -187,6 +188,16 @@ char *filename_to_url(const char *filename) {
|
|||||||
/* Put a pseudo-root directory before "C:/" or whatever. */
|
/* Put a pseudo-root directory before "C:/" or whatever. */
|
||||||
url = "/" + url;
|
url = "/" + url;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Percent-encode any troublesome characters. */
|
||||||
|
int i = 0;
|
||||||
|
/* See RFC 3986, section 3.3 "Path" for allowed characters. */
|
||||||
|
while ((i = url.find_first_of("?#[]", i)) != std::string::npos) {
|
||||||
|
Snprintf(percent_buffer, sizeof(percent_buffer), "%%%02X", url[i]);
|
||||||
|
url.replace(i, 1, percent_buffer);
|
||||||
|
i += strlen(percent_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
url = "file://" + url;
|
url = "file://" + url;
|
||||||
|
|
||||||
return strdup(url.c_str());
|
return strdup(url.c_str());
|
||||||
|
|||||||
Reference in New Issue
Block a user