1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-11-02 08:57:19 -04:00

[uri] Introduced function encode_uri_string_percent. Refs #221

This function is similar to encode_uri_string, but does not
encode percents.
This commit is contained in:
Witold Filipczyk 2023-04-23 15:05:58 +02:00
parent 4a3fa85ee7
commit 7a665d8de4
3 changed files with 41 additions and 5 deletions

View File

@ -350,10 +350,22 @@ add_url_to_http_string(struct string *header, struct uri *uri, uri_component_T c
* before. We should probably encode all URLs as early as * before. We should probably encode all URLs as early as
* possible, and possibly decode them back in protocol * possible, and possibly decode them back in protocol
* backends. --pasky */ * backends. --pasky */
char *string = get_uri_string(uri, components); struct string encoded;
char *data = string; char *string;
char *data;
if (!string) return; if (!init_string(&encoded)) {
return;
}
string = get_uri_string(uri, components);
if (!string) {
done_string(&encoded);
return;
}
encode_uri_string_percent(&encoded, string, -1);
mem_free(string);
data = encoded.source;
while (*data) { while (*data) {
int len = strcspn(data, " \t\r\n\\"); int len = strcspn(data, " \t\r\n\\");
@ -369,8 +381,7 @@ add_url_to_http_string(struct string *header, struct uri *uri, uri_component_T c
data += len; data += len;
} }
done_string(&encoded);
mem_free(string);
} }
/* Parse from @end - 1 to @start and set *@value to integer found. /* Parse from @end - 1 to @start and set *@value to integer found.

View File

@ -1422,6 +1422,29 @@ encode_uri_string(struct string *string, const char *name, int namelen,
} }
} }
void
encode_uri_string_percent(struct string *string, const char *name, int namelen)
{
char n[4];
const char *end;
n[0] = '%';
n[3] = '\0';
if (namelen < 0) namelen = strlen(name);
for (end = name + namelen; name < end; name++) {
if (safe_char(*name) || (*name == '/') || (*name == '%')) {
add_char_to_string(string, *name);
} else {
/* Hex it. */
n[1] = Hx((((int) *name) & 0xF0) >> 4);
n[2] = Hx(((int) *name) & 0xF);
add_bytes_to_string(string, n, sizeof(n) - 1);
}
}
}
void void
encode_win32_uri_string(struct string *string, char *name, int namelen) encode_win32_uri_string(struct string *string, char *name, int namelen)
{ {

View File

@ -321,6 +321,8 @@ int get_uri_port(const struct uri *uri);
void encode_uri_string(struct string *string, const char *name, int namelen, void encode_uri_string(struct string *string, const char *name, int namelen,
int convert_slashes); int convert_slashes);
void encode_uri_string_percent(struct string *string, const char *name, int namelen);
/* special version for Windows directory listing */ /* special version for Windows directory listing */
void encode_win32_uri_string(struct string *string, char *name, int namelen); void encode_win32_uri_string(struct string *string, char *name, int namelen);