1
0
mirror of https://github.com/lammertb/libhttp.git synced 2026-01-27 08:02:47 +03:00

Moved url_decode to own file

This commit is contained in:
Lammert Bies
2016-12-11 12:32:21 +01:00
parent 7bbb0b17d1
commit 331bf150dd
3 changed files with 60 additions and 28 deletions

View File

@@ -163,6 +163,7 @@ LIB_SOURCES = src/libhttp.c \
src/httplib_timer.c \
src/httplib_tls_dtor.c \
src/httplib_uninitialize_ssl.c \
src/httplib_url_decode.c \
src/httplib_url_encode.c \
src/httplib_version.c \
src/httplib_websocket_client_thread.c \

56
src/httplib_url_decode.c Normal file
View File

@@ -0,0 +1,56 @@
/*
* Copyright (c) 2016 Lammert Bies
* Copyright (c) 2013-2016 the Civetweb developers
* Copyright (c) 2004-2013 Sergey Lyubka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "libhttp-private.h"
int mg_url_decode( const char *src, int src_len, char *dst, int dst_len, int is_form_url_encoded ) {
int i;
int j;
int a;
int b;
#define HEXTOI(x) (isdigit(x) ? (x - '0') : (x - 'W'))
for (i = j = 0; (i < src_len) && (j < (dst_len - 1)); i++, j++) {
if (i < src_len - 2 && src[i] == '%'
&& 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;
} /* mg_url_decode */

View File

@@ -3165,10 +3165,11 @@ int XX_httplib_vprintf( struct mg_connection *conn, const char *fmt, va_list ap
if (buf != mem && buf != NULL) XX_httplib_free(buf);
return len;
} /* XX_httplib_vprintf */
int mg_printf(struct mg_connection *conn, const char *fmt, ...) {
int mg_printf( struct mg_connection *conn, const char *fmt, ... ) {
va_list ap;
int result;
@@ -3178,31 +3179,5 @@ int mg_printf(struct mg_connection *conn, const char *fmt, ...) {
va_end(ap);
return result;
}
int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int is_form_url_encoded) {
int i;
int j;
int a;
int b;
#define HEXTOI(x) (isdigit(x) ? (x - '0') : (x - 'W'))
for (i = j = 0; (i < src_len) && (j < (dst_len - 1)); i++, j++) {
if (i < src_len - 2 && src[i] == '%'
&& 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;
}
} /* mg_printf */