mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-15 10:29:47 +00:00
mg_url_decode() moved to string.c
This commit is contained in:
1 parent
886dcb3f5d
commit
71dd7a96c7
3 files changed
+50
-50
No files matched your search
@@ -259,31 +259,6 @@ int mg_write(struct mg_connection *conn, const void *buf, int len) {
|
||||
(int64_t) len);
|
||||
}
|
||||
|
||||
int mg_url_decode(const char *src, int src_len, char *dst,
|
||||
int dst_len, int is_form_url_encoded) {
|
||||
int i, j, a, b;
|
||||
#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
|
||||
|
||||
for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++) {
|
||||
if (src[i] == '%' && i < src_len - 2 &&
|
||||
isxdigit(* (const unsigned char *) (src + i + 1)) &&
|
||||
isxdigit(* (const unsigned char *) (src + i + 2))) {
|
||||
a = tolower(* (const unsigned char *) (src + i + 1));
|
||||
b = tolower(* (const unsigned char *) (src + i + 2));
|
||||
dst[j] = (char) ((HEXTOI(a) << 4) | HEXTOI(b));
|
||||
i += 2;
|
||||
} else if (is_form_url_encoded && src[i] == '+') {
|
||||
dst[j] = ' ';
|
||||
} else {
|
||||
dst[j] = src[i];
|
||||
}
|
||||
}
|
||||
|
||||
dst[j] = '\0'; // Null-terminate the destination
|
||||
|
||||
return i >= src_len ? j : -1;
|
||||
}
|
||||
|
||||
int mg_get_var(const char *data, size_t data_len, const char *name,
|
||||
char *dst, size_t dst_len) {
|
||||
const char *p, *e, *s;
|
||||
|
||||
@@ -286,6 +286,31 @@ void mg_url_encode(const char *src, char *dst, size_t dst_len) {
|
||||
*dst = '\0';
|
||||
}
|
||||
|
||||
int mg_url_decode(const char *src, int src_len, char *dst,
|
||||
int dst_len, int is_form_url_encoded) {
|
||||
int i, j, a, b;
|
||||
#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
|
||||
|
||||
for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++) {
|
||||
if (src[i] == '%' && i < src_len - 2 &&
|
||||
isxdigit(* (const unsigned char *) (src + i + 1)) &&
|
||||
isxdigit(* (const unsigned char *) (src + i + 2))) {
|
||||
a = tolower(* (const unsigned char *) (src + i + 1));
|
||||
b = tolower(* (const unsigned char *) (src + i + 2));
|
||||
dst[j] = (char) ((HEXTOI(a) << 4) | HEXTOI(b));
|
||||
i += 2;
|
||||
} else if (is_form_url_encoded && src[i] == '+') {
|
||||
dst[j] = ' ';
|
||||
} else {
|
||||
dst[j] = src[i];
|
||||
}
|
||||
}
|
||||
|
||||
dst[j] = '\0'; // Null-terminate the destination
|
||||
|
||||
return i >= src_len ? j : -1;
|
||||
}
|
||||
|
||||
// Check whether full request is buffered. Return:
|
||||
// -1 if request is malformed
|
||||
// 0 if request is not yet fully buffered
|
||||
|
||||
+25
-25
@@ -766,6 +766,31 @@ void mg_url_encode(const char *src, char *dst, size_t dst_len) {
|
||||
*dst = '\0';
|
||||
}
|
||||
|
||||
int mg_url_decode(const char *src, int src_len, char *dst,
|
||||
int dst_len, int is_form_url_encoded) {
|
||||
int i, j, a, b;
|
||||
#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
|
||||
|
||||
for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++) {
|
||||
if (src[i] == '%' && i < src_len - 2 &&
|
||||
isxdigit(* (const unsigned char *) (src + i + 1)) &&
|
||||
isxdigit(* (const unsigned char *) (src + i + 2))) {
|
||||
a = tolower(* (const unsigned char *) (src + i + 1));
|
||||
b = tolower(* (const unsigned char *) (src + i + 2));
|
||||
dst[j] = (char) ((HEXTOI(a) << 4) | HEXTOI(b));
|
||||
i += 2;
|
||||
} else if (is_form_url_encoded && src[i] == '+') {
|
||||
dst[j] = ' ';
|
||||
} else {
|
||||
dst[j] = src[i];
|
||||
}
|
||||
}
|
||||
|
||||
dst[j] = '\0'; // Null-terminate the destination
|
||||
|
||||
return i >= src_len ? j : -1;
|
||||
}
|
||||
|
||||
// Check whether full request is buffered. Return:
|
||||
// -1 if request is malformed
|
||||
// 0 if request is not yet fully buffered
|
||||
@@ -2828,31 +2853,6 @@ int mg_write(struct mg_connection *conn, const void *buf, int len) {
|
||||
(int64_t) len);
|
||||
}
|
||||
|
||||
int mg_url_decode(const char *src, int src_len, char *dst,
|
||||
int dst_len, int is_form_url_encoded) {
|
||||
int i, j, a, b;
|
||||
#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
|
||||
|
||||
for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++) {
|
||||
if (src[i] == '%' && i < src_len - 2 &&
|
||||
isxdigit(* (const unsigned char *) (src + i + 1)) &&
|
||||
isxdigit(* (const unsigned char *) (src + i + 2))) {
|
||||
a = tolower(* (const unsigned char *) (src + i + 1));
|
||||
b = tolower(* (const unsigned char *) (src + i + 2));
|
||||
dst[j] = (char) ((HEXTOI(a) << 4) | HEXTOI(b));
|
||||
i += 2;
|
||||
} else if (is_form_url_encoded && src[i] == '+') {
|
||||
dst[j] = ' ';
|
||||
} else {
|
||||
dst[j] = src[i];
|
||||
}
|
||||
}
|
||||
|
||||
dst[j] = '\0'; // Null-terminate the destination
|
||||
|
||||
return i >= src_len ? j : -1;
|
||||
}
|
||||
|
||||
int mg_get_var(const char *data, size_t data_len, const char *name,
|
||||
char *dst, size_t dst_len) {
|
||||
const char *p, *e, *s;
|
||||
|
||||
Reference in new issue
Block a user