2014-09-17 11:20:23 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2014 Johannes Schauer <j.schauer@email.de>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License version 2, as published by
|
|
|
|
|
* the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
|
* more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc., 51
|
|
|
|
|
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2014-09-18 09:11:00 +02:00
|
|
|
#include <curl/curl.h>
|
2014-09-19 21:14:30 +02:00
|
|
|
#include <curl/easy.h>
|
2014-09-18 09:11:00 +02:00
|
|
|
#include <stdbool.h>
|
2014-12-04 10:34:54 +01:00
|
|
|
#include <stdio.h>
|
2014-09-17 11:20:23 +02:00
|
|
|
|
2014-09-18 09:11:00 +02:00
|
|
|
#include "http.h"
|
2014-09-17 11:20:23 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
static int http_progress_cb(void *user_ptr, double dltotal, double dlnow,
|
|
|
|
|
double ultotal, double ulnow);
|
|
|
|
|
static size_t http_read_buf_cb(char *data, size_t size, size_t nmemb,
|
|
|
|
|
void *user_ptr);
|
2014-12-04 10:34:54 +01:00
|
|
|
static size_t http_read_file_cb(char *data, size_t size, size_t nmemb,
|
|
|
|
|
void *user_ptr);
|
2014-09-20 10:59:54 +02:00
|
|
|
static size_t http_write_buf_cb(char *data, size_t size, size_t nmemb,
|
|
|
|
|
void *user_ptr);
|
|
|
|
|
static size_t http_write_file_cb(char *data, size_t size, size_t nmemb,
|
|
|
|
|
void *user_ptr);
|
|
|
|
|
|
|
|
|
|
struct mfhttp {
|
|
|
|
|
CURL *curl_handle;
|
|
|
|
|
char *write_buf;
|
|
|
|
|
size_t write_buf_len;
|
|
|
|
|
double ul_len;
|
|
|
|
|
double ul_now;
|
|
|
|
|
double dl_len;
|
|
|
|
|
double dl_now;
|
|
|
|
|
bool show_progress;
|
|
|
|
|
char error_buf[CURL_ERROR_SIZE];
|
|
|
|
|
FILE *stream;
|
2014-09-18 09:11:00 +02:00
|
|
|
};
|
2014-09-17 11:20:23 +02:00
|
|
|
|
|
|
|
|
/*
|
2014-09-19 23:13:29 +02:00
|
|
|
* This set of functions is made such that the mfhttp struct and the curl
|
2014-09-17 11:20:23 +02:00
|
|
|
* handle it stores can be reused for multiple operations
|
|
|
|
|
*
|
|
|
|
|
* We do not use a single global instance because mediafire does not support
|
|
|
|
|
* keep-alive anyways.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-12-05 13:09:56 +01:00
|
|
|
static void http_curl_reset(mfhttp * conn)
|
|
|
|
|
{
|
|
|
|
|
curl_easy_reset(conn->curl_handle);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_NOPROGRESS, 0);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_PROGRESSFUNCTION,
|
|
|
|
|
http_progress_cb);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_PROGRESSDATA, (void *)conn);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_FOLLOWLOCATION, 1);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_SSLENGINE, NULL);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_SSLENGINE_DEFAULT, 1L);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_ERRORBUFFER, conn->error_buf);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_PROXY, getenv("http_proxy"));
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_VERBOSE, 0L);
|
2014-12-28 14:00:23 -06:00
|
|
|
|
|
|
|
|
// it should never take 5 seconds to establish a connection to the server
|
2014-12-29 22:08:10 -06:00
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_CONNECTTIMEOUT, 5);
|
2014-12-28 14:00:23 -06:00
|
|
|
|
2014-12-05 13:48:47 +01:00
|
|
|
//curl_easy_setopt(conn->curl_handle, CURLOPT_SSL_VERIFYPEER, 0L);
|
2014-12-05 13:09:56 +01:00
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
mfhttp *http_create(void)
|
2014-09-17 11:20:23 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
mfhttp *conn;
|
|
|
|
|
CURL *curl_handle;
|
|
|
|
|
|
2014-09-17 11:20:23 +02:00
|
|
|
curl_handle = curl_easy_init();
|
2014-09-20 10:59:54 +02:00
|
|
|
if (curl_handle == NULL)
|
|
|
|
|
return NULL;
|
2014-09-17 11:20:23 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
conn = (mfhttp *) calloc(1, sizeof(mfhttp));
|
2014-09-17 11:20:23 +02:00
|
|
|
conn->curl_handle = curl_handle;
|
|
|
|
|
|
|
|
|
|
conn->show_progress = false;
|
|
|
|
|
return conn;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
json_t *http_parse_buf_json(mfhttp * conn, size_t flags,
|
|
|
|
|
json_error_t * error)
|
2014-09-18 09:11:00 +02:00
|
|
|
{
|
|
|
|
|
return json_loadb(conn->write_buf, conn->write_buf_len, flags, error);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
void http_destroy(mfhttp * conn)
|
2014-09-17 11:20:23 +02:00
|
|
|
{
|
|
|
|
|
curl_easy_cleanup(conn->curl_handle);
|
|
|
|
|
free(conn->write_buf);
|
|
|
|
|
free(conn);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
static int
|
|
|
|
|
http_progress_cb(void *user_ptr, double dltotal, double dlnow,
|
|
|
|
|
double ultotal, double ulnow)
|
2014-09-17 11:20:23 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
mfhttp *conn;
|
2014-09-17 11:20:23 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (user_ptr == NULL)
|
|
|
|
|
return 0;
|
2014-09-17 11:20:23 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
conn = (mfhttp *) user_ptr;
|
2014-09-17 11:20:23 +02:00
|
|
|
|
|
|
|
|
conn->ul_len = ultotal;
|
|
|
|
|
conn->ul_now = ulnow;
|
|
|
|
|
|
|
|
|
|
conn->dl_len = dltotal;
|
|
|
|
|
conn->dl_now = dlnow;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2014-09-20 10:59:54 +02:00
|
|
|
http_get_buf(mfhttp * conn, const char *url,
|
|
|
|
|
int (*data_handler) (mfhttp * conn, void *data), void *data)
|
2014-09-17 11:20:23 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
int retval;
|
|
|
|
|
|
2014-12-05 13:09:56 +01:00
|
|
|
http_curl_reset(conn);
|
2014-12-04 10:34:54 +01:00
|
|
|
conn->write_buf_len = 0;
|
2014-09-17 11:20:23 +02:00
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
|
2014-09-25 19:38:48 +02:00
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION,
|
|
|
|
|
http_read_buf_cb);
|
2014-09-20 10:59:54 +02:00
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void *)conn);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION,
|
|
|
|
|
http_write_buf_cb);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void *)conn);
|
2014-09-24 10:48:54 +02:00
|
|
|
fprintf(stderr, "GET: %s\n", url);
|
2014-09-17 11:20:23 +02:00
|
|
|
retval = curl_easy_perform(conn->curl_handle);
|
2014-09-20 10:59:54 +02:00
|
|
|
if (retval != CURLE_OK) {
|
2014-09-17 11:20:23 +02:00
|
|
|
fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf);
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
if (data_handler != NULL)
|
|
|
|
|
retval = data_handler(conn, data);
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
static size_t
|
2014-09-18 09:11:00 +02:00
|
|
|
http_read_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
2014-09-17 11:20:23 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
size_t data_len;
|
2014-09-17 11:20:23 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (user_ptr == NULL)
|
|
|
|
|
return 0;
|
2014-09-17 11:20:23 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
data_len = size * nmemb;
|
2014-09-17 11:20:23 +02:00
|
|
|
|
|
|
|
|
if (data_len > 0) {
|
2014-09-19 09:21:28 +02:00
|
|
|
fwrite(data, size, nmemb, stderr);
|
2014-09-17 11:20:23 +02:00
|
|
|
fprintf(stderr, "Not implemented");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
static size_t
|
2014-09-18 09:11:00 +02:00
|
|
|
http_write_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
2014-09-17 11:20:23 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
mfhttp *conn;
|
|
|
|
|
size_t data_len;
|
2014-09-17 11:20:23 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (user_ptr == NULL)
|
|
|
|
|
return 0;
|
2014-09-17 11:20:23 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
conn = (mfhttp *) user_ptr;
|
|
|
|
|
data_len = size * nmemb;
|
2014-09-17 11:20:23 +02:00
|
|
|
|
|
|
|
|
if (data_len > 0) {
|
2014-09-24 10:48:54 +02:00
|
|
|
fwrite(data, size, nmemb, stderr);
|
2014-09-17 11:20:23 +02:00
|
|
|
conn->write_buf = (char *)realloc(conn->write_buf,
|
2014-09-20 10:59:54 +02:00
|
|
|
conn->write_buf_len + data_len);
|
2014-09-17 11:20:23 +02:00
|
|
|
memcpy(conn->write_buf + conn->write_buf_len, data, data_len);
|
|
|
|
|
conn->write_buf_len += data_len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data_len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2014-09-20 10:59:54 +02:00
|
|
|
http_post_buf(mfhttp * conn, const char *url, const char *post_args,
|
|
|
|
|
int (*data_handler) (mfhttp * conn, void *data), void *data)
|
2014-09-17 11:20:23 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
int retval;
|
|
|
|
|
|
2014-12-05 13:09:56 +01:00
|
|
|
http_curl_reset(conn);
|
2014-12-04 10:34:54 +01:00
|
|
|
conn->write_buf_len = 0;
|
2014-09-17 11:20:23 +02:00
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
|
2014-09-25 19:38:48 +02:00
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION,
|
|
|
|
|
http_read_buf_cb);
|
2014-09-20 10:59:54 +02:00
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void *)conn);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION,
|
|
|
|
|
http_write_buf_cb);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void *)conn);
|
2014-09-17 11:20:23 +02:00
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_POSTFIELDS, post_args);
|
2014-09-25 13:05:17 +02:00
|
|
|
fprintf(stderr, "POST: %s\n", url);
|
2014-12-04 10:34:54 +01:00
|
|
|
retval = curl_easy_perform(conn->curl_handle);
|
2014-09-20 10:59:54 +02:00
|
|
|
if (retval != CURLE_OK) {
|
2014-10-25 11:33:55 +02:00
|
|
|
fprintf(stderr, "error curl_easy_perform \"%s\" \"%s\"\n\r",
|
|
|
|
|
curl_easy_strerror(retval), conn->error_buf);
|
2014-09-17 11:20:23 +02:00
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
if (data_handler != NULL)
|
|
|
|
|
retval = data_handler(conn, data);
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
int http_get_file(mfhttp * conn, const char *url, const char *path)
|
2014-09-17 11:20:23 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
int retval;
|
|
|
|
|
|
2014-12-05 13:09:56 +01:00
|
|
|
http_curl_reset(conn);
|
2014-09-17 11:20:23 +02:00
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
|
2014-09-25 19:38:48 +02:00
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION,
|
|
|
|
|
http_read_buf_cb);
|
2014-09-20 10:59:54 +02:00
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void *)conn);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION,
|
|
|
|
|
http_write_file_cb);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void *)conn);
|
2014-09-17 11:20:23 +02:00
|
|
|
// FIXME: handle fopen() return value
|
|
|
|
|
conn->stream = fopen(path, "w+");
|
2014-12-04 10:34:54 +01:00
|
|
|
fprintf(stderr, "GET: %s\n", url);
|
2014-09-17 11:20:23 +02:00
|
|
|
retval = curl_easy_perform(conn->curl_handle);
|
|
|
|
|
fclose(conn->stream);
|
2014-09-20 10:59:54 +02:00
|
|
|
if (retval != CURLE_OK) {
|
2014-09-17 11:20:23 +02:00
|
|
|
fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf);
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
static size_t
|
2014-09-18 09:11:00 +02:00
|
|
|
http_write_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
2014-09-17 11:20:23 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
mfhttp *conn;
|
2014-12-04 10:34:54 +01:00
|
|
|
size_t ret;
|
2014-09-17 11:20:23 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (user_ptr == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
conn = (mfhttp *) user_ptr;
|
2014-09-17 11:20:23 +02:00
|
|
|
|
2014-12-04 10:34:54 +01:00
|
|
|
ret = fwrite(data, size, nmemb, conn->stream);
|
2014-09-17 11:20:23 +02:00
|
|
|
|
|
|
|
|
fprintf(stderr, "\r %.0f / %.0f", conn->dl_now, conn->dl_len);
|
2014-09-19 09:21:28 +02:00
|
|
|
|
2014-12-04 10:34:54 +01:00
|
|
|
return size * ret;
|
2014-09-17 11:20:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-12-04 10:34:54 +01:00
|
|
|
static size_t
|
|
|
|
|
http_read_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
2014-09-17 11:20:23 +02:00
|
|
|
{
|
2014-12-04 10:34:54 +01:00
|
|
|
mfhttp *conn;
|
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
|
|
if (user_ptr == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
conn = (mfhttp *) user_ptr;
|
|
|
|
|
|
|
|
|
|
ret = fread(data, size, nmemb, conn->stream);
|
|
|
|
|
|
2014-12-21 07:47:12 +01:00
|
|
|
fprintf(stderr, "\r %.0f / %.0f", conn->ul_now, conn->ul_len);
|
2014-12-04 10:34:54 +01:00
|
|
|
|
|
|
|
|
return size * ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2014-12-04 16:07:12 +01:00
|
|
|
http_post_file(mfhttp * conn, const char *url, FILE * fh,
|
2014-12-07 15:03:38 +01:00
|
|
|
struct curl_slist **custom_headers, uint64_t filesize,
|
2014-12-04 10:34:54 +01:00
|
|
|
int (*data_handler) (mfhttp * conn, void *data), void *data)
|
|
|
|
|
{
|
|
|
|
|
int retval;
|
2014-12-07 15:38:50 +01:00
|
|
|
struct curl_slist *fallback_headers;
|
2014-12-04 10:34:54 +01:00
|
|
|
|
2014-12-05 13:09:56 +01:00
|
|
|
http_curl_reset(conn);
|
2014-12-04 10:34:54 +01:00
|
|
|
conn->write_buf_len = 0;
|
|
|
|
|
|
2014-12-07 15:38:50 +01:00
|
|
|
if (custom_headers == NULL) {
|
|
|
|
|
custom_headers = &fallback_headers;
|
2014-12-08 14:12:17 +01:00
|
|
|
*custom_headers = NULL;
|
2014-12-07 15:38:50 +01:00
|
|
|
}
|
2014-12-04 10:34:54 +01:00
|
|
|
// when using POST, curl implicitly sets
|
|
|
|
|
// Content-Type: application/x-www-form-urlencoded
|
|
|
|
|
// make sure it is set to application/octet-stream instead
|
2014-12-07 15:03:38 +01:00
|
|
|
*custom_headers =
|
|
|
|
|
curl_slist_append(*custom_headers,
|
|
|
|
|
"Content-Type: application/octet-stream");
|
2014-12-04 10:34:54 +01:00
|
|
|
// when using POST, curl implicitly sets Expect: 100-continue
|
|
|
|
|
// make sure it is not set
|
2014-12-07 15:03:38 +01:00
|
|
|
*custom_headers = curl_slist_append(*custom_headers, "Expect:");
|
2014-12-04 10:34:54 +01:00
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_POST, 1);
|
2014-12-07 15:03:38 +01:00
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_HTTPHEADER, *custom_headers);
|
2014-12-04 10:34:54 +01:00
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION,
|
|
|
|
|
http_read_file_cb);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void *)conn);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION,
|
|
|
|
|
http_write_buf_cb);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void *)conn);
|
|
|
|
|
curl_easy_setopt(conn->curl_handle, CURLOPT_POSTFIELDSIZE, filesize);
|
|
|
|
|
|
2014-12-04 16:07:12 +01:00
|
|
|
conn->stream = fh;
|
2014-12-04 10:34:54 +01:00
|
|
|
fprintf(stderr, "POST: %s\n", url);
|
|
|
|
|
retval = curl_easy_perform(conn->curl_handle);
|
2014-12-07 15:03:38 +01:00
|
|
|
curl_slist_free_all(*custom_headers);
|
|
|
|
|
*custom_headers = NULL;
|
2014-12-04 10:34:54 +01:00
|
|
|
if (retval != CURLE_OK) {
|
|
|
|
|
fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf);
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
if (data_handler != NULL)
|
|
|
|
|
retval = data_handler(conn, data);
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
2015-01-16 08:49:23 +01:00
|
|
|
|
|
|
|
|
// we roll our own urlencode function because curl_easy_escape requires a curl
|
|
|
|
|
// handle
|
|
|
|
|
char *urlencode(const char *inp)
|
|
|
|
|
{
|
|
|
|
|
char *buf;
|
|
|
|
|
char *bufp;
|
|
|
|
|
char hex[] = "0123456789abcdef";
|
|
|
|
|
|
|
|
|
|
// allocating three times the length of the input because in the worst
|
|
|
|
|
// case each character must be urlencoded and add a byte for the
|
|
|
|
|
// terminating zero
|
|
|
|
|
bufp = buf = (char *)malloc(strlen(inp) * 3 + 1);
|
|
|
|
|
|
|
|
|
|
if (buf == NULL) {
|
|
|
|
|
fprintf(stderr, "malloc failed\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (*inp) {
|
|
|
|
|
if ((*inp >= '0' && *inp <= '9')
|
|
|
|
|
|| (*inp >= 'A' && *inp <= 'Z')
|
|
|
|
|
|| (*inp >= 'a' && *inp <= 'z')
|
|
|
|
|
|| *inp == '-' || *inp == '_' || *inp == '.' || *inp == '~') {
|
|
|
|
|
*bufp++ = *inp;
|
|
|
|
|
} else {
|
|
|
|
|
*bufp++ = '%';
|
|
|
|
|
*bufp++ = hex[(*inp >> 4) & 0xf];
|
|
|
|
|
*bufp++ = hex[*inp & 0xf];
|
|
|
|
|
}
|
|
|
|
|
inp++;
|
|
|
|
|
}
|
|
|
|
|
*bufp = '\0';
|
|
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
|
}
|