1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-19 09:42:06 +03:00

Added URLEncode methods

This commit is contained in:
Thomas Davis
2013-08-31 19:30:05 -04:00
parent 466f3982fe
commit acbe6db15f
5 changed files with 93 additions and 14 deletions

View File

@@ -4,6 +4,7 @@ Release Notes v1.4 (UNDER DEVELOPMENT)
- Added CivetServer::getParam methods - Added CivetServer::getParam methods
- Added CivetServer::urlDecode methods - Added CivetServer::urlDecode methods
- Added CivetServer::urlEncode methods
- Dealt with compiler warnings and some static analysis hits. - Dealt with compiler warnings and some static analysis hits.
- Added mg_get_var2() to parse repeated query variables - Added mg_get_var2() to parse repeated query variables
- Externalized logging function cry() as mg_cry() - Externalized logging function cry() as mg_cry()

View File

@@ -223,7 +223,7 @@ public:
/** /**
* urlDecode(const char *, int, std::string &, bool) * urlDecode(const std::string &, std::string &, bool)
* *
* @param src string to be decoded * @param src string to be decoded
* @param dst destination string * @param dst destination string
@@ -237,7 +237,7 @@ public:
} }
/** /**
* urlDecode(const char *, int, std::string &, bool) * urlDecode(const char *, size_t, std::string &, bool)
* *
* @param src buffer to be decoded * @param src buffer to be decoded
* @param src_len length of buffer to be decoded * @param src_len length of buffer to be decoded
@@ -249,6 +249,48 @@ public:
*/ */
static void urlDecode(const char *src, size_t src_len, std::string &dst, bool is_form_url_encoded=true); static void urlDecode(const char *src, size_t src_len, std::string &dst, bool is_form_url_encoded=true);
/**
* urlDecode(const char *, std::string &, bool)
*
* @param src buffer to be decoded (0 terminated)
* @param dst destination string
* @is_form_url_encoded true if form url encoded
* form-url-encoded data differs from URI encoding in a way that it
* uses '+' as character for space, see RFC 1866 section 8.2.1
* http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
*/
static void urlDecode(const char *src, std::string &dst, bool is_form_url_encoded=true);
/**
* urlEncode(const std::string &, std::string &, bool)
*
* @param src buffer to be encoded
* @param dst destination string
* @append true if string should not be cleared before encoding.
*/
static void urlEncode(const std::string &src, std::string &dst, bool append=false) {
urlEncode(src.c_str(), src.length(), dst, append);
}
/**
* urlEncode(const char *, size_t, std::string &, bool)
*
* @param src buffer to be encoded (0 terminated)
* @param dst destination string
* @append true if string should not be cleared before encoding.
*/
static void urlEncode(const char *src, std::string &dst, bool append=false);
/**
* urlEncode(const char *, size_t, std::string &, bool)
*
* @param src buffer to be encoded
* @param src_len length of buffer to be decoded
* @param dst destination string
* @append true if string should not be cleared before encoding.
*/
static void urlEncode(const char *src, size_t src_len, std::string &dst, bool append=false);
protected: protected:
/** /**

View File

@@ -409,6 +409,11 @@ const char *mg_version(void);
int mg_url_decode(const char *src, int src_len, char *dst, int mg_url_decode(const char *src, int src_len, char *dst,
int dst_len, int is_form_url_encoded); int dst_len, int is_form_url_encoded);
// URL-encode input buffer into destination buffer.
// returns the length of the resulting buffer or -1
// is the buffer is too small.
int mg_url_encode(const char *src, char *dst, size_t dst_len);
// MD5 hash given strings. // MD5 hash given strings.
// Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of // Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
// ASCIIz strings. When function returns, buf will contain human-readable // ASCIIz strings. When function returns, buf will contain human-readable

View File

@@ -171,6 +171,11 @@ const char* CivetServer::getHeader(struct mg_connection *conn, const std::string
return mg_get_header(conn, headerName.c_str()); return mg_get_header(conn, headerName.c_str());
} }
void
CivetServer::urlDecode(const char *src, std::string &dst, bool is_form_url_encoded) {
urlDecode(src, strlen(src), dst, is_form_url_encoded);
}
void void
CivetServer::urlDecode(const char *src, size_t src_len, std::string &dst, bool is_form_url_encoded) { CivetServer::urlDecode(const char *src, size_t src_len, std::string &dst, bool is_form_url_encoded) {
int i, j, a, b; int i, j, a, b;
@@ -236,3 +241,27 @@ CivetServer::getParam(const char *data, size_t data_len, const char *name,
return false; return false;
} }
void
CivetServer::urlEncode(const char *src, std::string &dst, bool append) {
urlEncode(src, strlen(src), dst, append);
}
void
CivetServer::urlEncode(const char *src, size_t src_len, std::string &dst, bool append) {
static const char *dont_escape = "._-$,;~()";
static const char *hex = "0123456789abcdef";
if (!append)
dst.clear();
for (; src_len > 0; src_len--) {
if (isalnum(*(const unsigned char *) src) ||
strchr(dont_escape, * (const unsigned char *) src) != NULL) {
dst.push_back(*src);
} else {
dst.push_back('%');
dst.push_back(hex[(* (const unsigned char *) src) >> 4]);
dst.push_back(hex[(* (const unsigned char *) src) & 0xf]);
}
}
}

View File

@@ -2435,26 +2435,28 @@ static SOCKET conn2(const char *host, int port, int use_ssl,
return sock; return sock;
} }
int mg_url_encode(const char *src, char *dst, size_t dst_len) {
void mg_url_encode(const char *src, char *dst, size_t dst_len) {
static const char *dont_escape = "._-$,;~()"; static const char *dont_escape = "._-$,;~()";
static const char *hex = "0123456789abcdef"; static const char *hex = "0123456789abcdef";
char *pos = dst;
const char *end = dst + dst_len - 1; const char *end = dst + dst_len - 1;
for (; *src != '\0' && dst < end; src++, dst++) { for (; *src != '\0' && pos < end; src++, pos++) {
if (isalnum(*(const unsigned char *) src) || if (isalnum(*(const unsigned char *) src) ||
strchr(dont_escape, * (const unsigned char *) src) != NULL) { strchr(dont_escape, * (const unsigned char *) src) != NULL) {
*dst = *src; *pos = *src;
} else if (dst + 2 < end) { } else if (pos + 2 < end) {
dst[0] = '%'; pos[0] = '%';
dst[1] = hex[(* (const unsigned char *) src) >> 4]; pos[1] = hex[(* (const unsigned char *) src) >> 4];
dst[2] = hex[(* (const unsigned char *) src) & 0xf]; pos[2] = hex[(* (const unsigned char *) src) & 0xf];
dst += 2; pos += 2;
} } else {
return -1;
}
} }
*dst = '\0'; *pos = '\0';
return (*src == '\0') ? (int)(pos - dst) : -1;
} }
static void print_dir_entry(struct de *de) { static void print_dir_entry(struct de *de) {