Bump uclient version 2016-12-09
This commit is contained in:
14
bsp/buildroot_external/package/uclient/Config.in
Normal file
14
bsp/buildroot_external/package/uclient/Config.in
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
config BR2_PACKAGE_LIBUBOX
|
||||||
|
bool "libubox"
|
||||||
|
depends on !BR2_STATIC_LIBS
|
||||||
|
select BR2_PACKAGE_JSON_C
|
||||||
|
help
|
||||||
|
This library originates from the OpenWrt project to
|
||||||
|
handle the configuration file infrastructure, but can
|
||||||
|
also be used for the same purposes in projects other
|
||||||
|
than OpenWrt.
|
||||||
|
|
||||||
|
http://nbd.name/gitweb.cgi?p=luci2/libubox.git;a=summary
|
||||||
|
|
||||||
|
comment "libubox needs a toolchain w/ dynamic library"
|
||||||
|
depends on BR2_STATIC_LIBS
|
||||||
24
bsp/buildroot_external/package/uclient/uclient.mk
Normal file
24
bsp/buildroot_external/package/uclient/uclient.mk
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
################################################################################
|
||||||
|
#
|
||||||
|
# LIB UBOX
|
||||||
|
#
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
UCLIENT_VERSION:= 2016-12-09
|
||||||
|
|
||||||
|
UCLIENT_SITE = $(TOPDIR)/../../src/3P/libubox
|
||||||
|
UCLIENT_SITE_METHOD = local
|
||||||
|
UCLIENT_LICENSE = LGPLv2.1, GPLv2, BSD-3c, MIT
|
||||||
|
UCLIENT_INSTALL_STAGING = YES
|
||||||
|
|
||||||
|
UCLIENT_DEPENDENCIES = json-c
|
||||||
|
|
||||||
|
UCLIENT_CONF = SRC_DIR=$(TOPDIR)/../..
|
||||||
|
|
||||||
|
UCLIENT_CONF_OPTS +=-DBUILD_LUA=OFF
|
||||||
|
|
||||||
|
UCLIENT_CONF_ENV = $(UCLIENT_CONF)
|
||||||
|
UCLIENT_MAKE_ENV = $(UCLIENT_CONF)
|
||||||
|
UCLIENT_CONF_OPTS += -DMODULE_PATH=$(TOPDIR)/../../bsp/cmake-modules -DCMAKE_BUILD_TYPE=$(BUILD_TYPE)
|
||||||
|
|
||||||
|
$(eval $(cmake-package))
|
||||||
9
src/3P/uclient/.gitignore
vendored
Normal file
9
src/3P/uclient/.gitignore
vendored
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
Makefile
|
||||||
|
CMakeCache.txt
|
||||||
|
CMakeFiles
|
||||||
|
*.cmake
|
||||||
|
*.a
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
install_manifest.txt
|
||||||
|
uclient-fetch
|
||||||
25
src/3P/uclient/CMakeLists.txt
Normal file
25
src/3P/uclient/CMakeLists.txt
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
cmake_minimum_required(VERSION 2.6)
|
||||||
|
|
||||||
|
INCLUDE(CheckIncludeFiles)
|
||||||
|
|
||||||
|
PROJECT(uclient C)
|
||||||
|
ADD_DEFINITIONS(-Os -Wall -Werror --std=gnu99 -g3 -Wmissing-declarations)
|
||||||
|
|
||||||
|
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
|
||||||
|
|
||||||
|
FIND_PATH(ubox_include_dir libubox/ustream-ssl.h)
|
||||||
|
INCLUDE_DIRECTORIES(${ubox_include_dir})
|
||||||
|
|
||||||
|
ADD_LIBRARY(uclient SHARED uclient.c uclient-http.c uclient-utils.c)
|
||||||
|
TARGET_LINK_LIBRARIES(uclient ubox dl)
|
||||||
|
|
||||||
|
ADD_EXECUTABLE(uclient-fetch uclient-fetch.c progress.c)
|
||||||
|
TARGET_LINK_LIBRARIES(uclient-fetch uclient)
|
||||||
|
|
||||||
|
INSTALL(FILES uclient.h uclient-utils.h
|
||||||
|
DESTINATION include/libubox
|
||||||
|
)
|
||||||
|
INSTALL(TARGETS uclient uclient-fetch
|
||||||
|
LIBRARY DESTINATION lib
|
||||||
|
RUNTIME DESTINATION bin
|
||||||
|
)
|
||||||
229
src/3P/uclient/progress.c
Normal file
229
src/3P/uclient/progress.c
Normal file
@@ -0,0 +1,229 @@
|
|||||||
|
/* vi: set sw=4 ts=4: */
|
||||||
|
/*
|
||||||
|
* Progress bar code.
|
||||||
|
*/
|
||||||
|
/* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
|
||||||
|
* much of which was blatantly stolen from openssh.
|
||||||
|
*/
|
||||||
|
/*-
|
||||||
|
* Copyright (c) 1992, 1993
|
||||||
|
* The Regents of the University of California. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* 3. BSD Advertising Clause omitted per the July 22, 1999 licensing change
|
||||||
|
* ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
|
||||||
|
*
|
||||||
|
* 4. Neither the name of the University nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <libubox/utils.h>
|
||||||
|
#include "progress.h"
|
||||||
|
|
||||||
|
enum {
|
||||||
|
/* Seconds when xfer considered "stalled" */
|
||||||
|
STALLTIME = 5
|
||||||
|
};
|
||||||
|
|
||||||
|
static int
|
||||||
|
wh_helper(int value, int def_val, const char *env_name)
|
||||||
|
{
|
||||||
|
if (value == 0) {
|
||||||
|
char *s = getenv(env_name);
|
||||||
|
if (s)
|
||||||
|
value = atoi(s);
|
||||||
|
}
|
||||||
|
if (value <= 1 || value >= 30000)
|
||||||
|
value = def_val;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int
|
||||||
|
get_tty2_width(void)
|
||||||
|
{
|
||||||
|
#ifndef TIOCGWINSZ
|
||||||
|
return wh_helper(0, 80, "COLUMNS");
|
||||||
|
#else
|
||||||
|
struct winsize win = {0};
|
||||||
|
ioctl(2, TIOCGWINSZ, &win);
|
||||||
|
return wh_helper(win.ws_col, 80, "COLUMNS");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int
|
||||||
|
monotonic_sec(void)
|
||||||
|
{
|
||||||
|
struct timespec tv;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &tv);
|
||||||
|
return tv.tv_sec;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
progress_init(struct progress *p, const char *curfile)
|
||||||
|
{
|
||||||
|
p->curfile = strdup(curfile);
|
||||||
|
p->start_sec = monotonic_sec();
|
||||||
|
p->last_update_sec = p->start_sec;
|
||||||
|
p->last_change_sec = p->start_sec;
|
||||||
|
p->last_size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* File already had beg_size bytes.
|
||||||
|
* Then we started downloading.
|
||||||
|
* We downloaded "transferred" bytes so far.
|
||||||
|
* Download is expected to stop when total size (beg_size + transferred)
|
||||||
|
* will be "totalsize" bytes.
|
||||||
|
* If totalsize == 0, then it is unknown.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
progress_update(struct progress *p, off_t beg_size,
|
||||||
|
off_t transferred, off_t totalsize)
|
||||||
|
{
|
||||||
|
off_t beg_and_transferred;
|
||||||
|
unsigned since_last_update, elapsed;
|
||||||
|
int barlength;
|
||||||
|
int kiloscale;
|
||||||
|
|
||||||
|
//transferred = 1234; /* use for stall detection testing */
|
||||||
|
//totalsize = 0; /* use for unknown size download testing */
|
||||||
|
|
||||||
|
elapsed = monotonic_sec();
|
||||||
|
since_last_update = elapsed - p->last_update_sec;
|
||||||
|
p->last_update_sec = elapsed;
|
||||||
|
|
||||||
|
if (totalsize != 0 && transferred >= totalsize - beg_size) {
|
||||||
|
/* Last call. Do not skip this update */
|
||||||
|
transferred = totalsize - beg_size; /* sanitize just in case */
|
||||||
|
}
|
||||||
|
else if (since_last_update == 0) {
|
||||||
|
/*
|
||||||
|
* Do not update on every call
|
||||||
|
* (we can be called on every network read!)
|
||||||
|
*/
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
kiloscale = 0;
|
||||||
|
/*
|
||||||
|
* Scale sizes down if they are close to overflowing.
|
||||||
|
* This allows calculations like (100 * transferred / totalsize)
|
||||||
|
* without risking overflow: we guarantee 10 highest bits to be 0.
|
||||||
|
* Introduced error is less than 1 / 2^12 ~= 0.025%
|
||||||
|
*/
|
||||||
|
if (ULONG_MAX > 0xffffffff || sizeof(off_t) == 4 || sizeof(off_t) != 8) {
|
||||||
|
/*
|
||||||
|
* 64-bit CPU || small off_t: in either case,
|
||||||
|
* >> is cheap, single-word operation.
|
||||||
|
* ... || strange off_t: also use this code
|
||||||
|
* (it is safe, just suboptimal wrt code size),
|
||||||
|
* because 32/64 optimized one works only for 64-bit off_t.
|
||||||
|
*/
|
||||||
|
if (totalsize >= (1 << 22)) {
|
||||||
|
totalsize >>= 10;
|
||||||
|
beg_size >>= 10;
|
||||||
|
transferred >>= 10;
|
||||||
|
kiloscale = 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* 32-bit CPU and 64-bit off_t.
|
||||||
|
* Use a 40-bit shift, it is easier to do on 32-bit CPU.
|
||||||
|
*/
|
||||||
|
/* ONE suppresses "warning: shift count >= width of type" */
|
||||||
|
#define ONE (sizeof(off_t) > 4)
|
||||||
|
if (totalsize >= (off_t)(1ULL << 54*ONE)) {
|
||||||
|
totalsize = (uint32_t)(totalsize >> 32*ONE) >> 8;
|
||||||
|
beg_size = (uint32_t)(beg_size >> 32*ONE) >> 8;
|
||||||
|
transferred = (uint32_t)(transferred >> 32*ONE) >> 8;
|
||||||
|
kiloscale = 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "\r%-20.20s", p->curfile);
|
||||||
|
|
||||||
|
beg_and_transferred = beg_size + transferred;
|
||||||
|
|
||||||
|
if (totalsize != 0) {
|
||||||
|
unsigned ratio = 100 * beg_and_transferred / totalsize;
|
||||||
|
fprintf(stderr, "%4u%%", ratio);
|
||||||
|
|
||||||
|
barlength = get_tty2_width() - 49;
|
||||||
|
if (barlength > 0) {
|
||||||
|
/* god bless gcc for variable arrays :) */
|
||||||
|
char buf[barlength + 1];
|
||||||
|
unsigned stars = (unsigned)barlength * beg_and_transferred / totalsize;
|
||||||
|
memset(buf, ' ', barlength);
|
||||||
|
buf[barlength] = '\0';
|
||||||
|
memset(buf, '*', stars);
|
||||||
|
fprintf(stderr, " |%s|", buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (beg_and_transferred >= 100000) {
|
||||||
|
beg_and_transferred >>= 10;
|
||||||
|
kiloscale++;
|
||||||
|
}
|
||||||
|
/* see http://en.wikipedia.org/wiki/Tera */
|
||||||
|
fprintf(stderr, "%6u%c", (unsigned)beg_and_transferred, " kMGTPEZY"[kiloscale]);
|
||||||
|
|
||||||
|
since_last_update = elapsed - p->last_change_sec;
|
||||||
|
if ((unsigned)transferred != p->last_size) {
|
||||||
|
p->last_change_sec = elapsed;
|
||||||
|
p->last_size = (unsigned)transferred;
|
||||||
|
if (since_last_update >= STALLTIME) {
|
||||||
|
/* We "cut out" these seconds from elapsed time
|
||||||
|
* by adjusting start time */
|
||||||
|
p->start_sec += since_last_update;
|
||||||
|
}
|
||||||
|
since_last_update = 0; /* we are un-stalled now */
|
||||||
|
}
|
||||||
|
|
||||||
|
elapsed -= p->start_sec; /* now it's "elapsed since start" */
|
||||||
|
|
||||||
|
if (since_last_update >= STALLTIME) {
|
||||||
|
fprintf(stderr, " - stalled -");
|
||||||
|
} else if (!totalsize || !transferred || (int)elapsed < 0) {
|
||||||
|
fprintf(stderr, " --:--:-- ETA");
|
||||||
|
} else {
|
||||||
|
unsigned eta, secs, hours;
|
||||||
|
|
||||||
|
totalsize -= beg_size; /* now it's "total to upload" */
|
||||||
|
|
||||||
|
/* Estimated remaining time =
|
||||||
|
* estimated_sec_to_dl_totalsize_bytes - elapsed_sec =
|
||||||
|
* totalsize / average_bytes_sec_so_far - elapsed =
|
||||||
|
* totalsize / (transferred/elapsed) - elapsed =
|
||||||
|
* totalsize * elapsed / transferred - elapsed
|
||||||
|
*/
|
||||||
|
eta = totalsize * elapsed / transferred - elapsed;
|
||||||
|
if (eta >= 1000*60*60)
|
||||||
|
eta = 1000*60*60 - 1;
|
||||||
|
secs = eta % 3600;
|
||||||
|
hours = eta / 3600;
|
||||||
|
fprintf(stderr, "%3u:%02u:%02u ETA", hours, secs / 60, secs % 60);
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/3P/uclient/progress.h
Normal file
25
src/3P/uclient/progress.h
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef __PROGRESS_H
|
||||||
|
#define __PROGRESS_H
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
struct progress {
|
||||||
|
unsigned int last_size;
|
||||||
|
unsigned int last_update_sec;
|
||||||
|
unsigned int last_change_sec;
|
||||||
|
unsigned int start_sec;
|
||||||
|
char *curfile;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void progress_init(struct progress *p, const char *curfile);
|
||||||
|
void progress_update(struct progress *p, off_t beg_size,
|
||||||
|
off_t transferred, off_t totalsize);
|
||||||
|
|
||||||
|
static inline void
|
||||||
|
progress_free(struct progress *p)
|
||||||
|
{
|
||||||
|
free(p->curfile);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
45
src/3P/uclient/uclient-backend.h
Normal file
45
src/3P/uclient/uclient-backend.h
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* uclient - ustream based protocol client library
|
||||||
|
*
|
||||||
|
* Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
#ifndef __UCLIENT_INTERNAL_H
|
||||||
|
#define __UCLIENT_INTERNAL_H
|
||||||
|
|
||||||
|
struct uclient_url;
|
||||||
|
|
||||||
|
struct uclient_backend {
|
||||||
|
const char * const * prefix;
|
||||||
|
|
||||||
|
struct uclient *(*alloc)(void);
|
||||||
|
void (*free)(struct uclient *cl);
|
||||||
|
void (*update_proxy_url)(struct uclient *cl);
|
||||||
|
void (*update_url)(struct uclient *cl);
|
||||||
|
|
||||||
|
int (*connect)(struct uclient *cl);
|
||||||
|
int (*request)(struct uclient *cl);
|
||||||
|
void (*disconnect)(struct uclient *cl);
|
||||||
|
|
||||||
|
int (*read)(struct uclient *cl, char *buf, unsigned int len);
|
||||||
|
int (*write)(struct uclient *cl, const char *buf, unsigned int len);
|
||||||
|
};
|
||||||
|
|
||||||
|
void uclient_backend_set_error(struct uclient *cl, int code);
|
||||||
|
void uclient_backend_set_eof(struct uclient *cl);
|
||||||
|
void uclient_backend_reset_state(struct uclient *cl);
|
||||||
|
struct uclient_url *uclient_get_url(const char *url_str, const char *auth_str);
|
||||||
|
struct uclient_url *uclient_get_url_location(struct uclient_url *url, const char *location);
|
||||||
|
|
||||||
|
#endif
|
||||||
717
src/3P/uclient/uclient-fetch.c
Normal file
717
src/3P/uclient/uclient-fetch.c
Normal file
@@ -0,0 +1,717 @@
|
|||||||
|
/*
|
||||||
|
* uclient - ustream based protocol client library
|
||||||
|
*
|
||||||
|
* Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define _GNU_SOURCE
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#include <getopt.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <glob.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
#include <libubox/blobmsg.h>
|
||||||
|
|
||||||
|
#include "progress.h"
|
||||||
|
#include "uclient.h"
|
||||||
|
#include "uclient-utils.h"
|
||||||
|
|
||||||
|
#ifdef __APPLE__
|
||||||
|
#define LIB_EXT "dylib"
|
||||||
|
#else
|
||||||
|
#define LIB_EXT "so"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static const char *user_agent = "uclient-fetch";
|
||||||
|
static const char *post_data;
|
||||||
|
static struct ustream_ssl_ctx *ssl_ctx;
|
||||||
|
static const struct ustream_ssl_ops *ssl_ops;
|
||||||
|
static int quiet = false;
|
||||||
|
static bool verify = true;
|
||||||
|
static bool proxy = true;
|
||||||
|
static bool default_certs = false;
|
||||||
|
static bool no_output;
|
||||||
|
static const char *output_file;
|
||||||
|
static int output_fd = -1;
|
||||||
|
static int error_ret;
|
||||||
|
static off_t out_offset;
|
||||||
|
static off_t out_bytes;
|
||||||
|
static off_t out_len;
|
||||||
|
static char *auth_str;
|
||||||
|
static char **urls;
|
||||||
|
static int n_urls;
|
||||||
|
static int timeout;
|
||||||
|
static bool resume, cur_resume;
|
||||||
|
|
||||||
|
static struct progress pmt;
|
||||||
|
static struct uloop_timeout pmt_timer;
|
||||||
|
|
||||||
|
static int init_request(struct uclient *cl);
|
||||||
|
static void request_done(struct uclient *cl);
|
||||||
|
|
||||||
|
static void pmt_update(struct uloop_timeout *t)
|
||||||
|
{
|
||||||
|
progress_update(&pmt, out_offset, out_bytes, out_len);
|
||||||
|
uloop_timeout_set(t, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *
|
||||||
|
get_proxy_url(char *url)
|
||||||
|
{
|
||||||
|
char prefix[16];
|
||||||
|
char *sep;
|
||||||
|
|
||||||
|
if (!proxy)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
sep = strchr(url, ':');
|
||||||
|
if (!sep)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (sep - url > 5)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
memcpy(prefix, url, sep - url);
|
||||||
|
strcpy(prefix + (sep - url), "_proxy");
|
||||||
|
return getenv(prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int open_output_file(const char *path, uint64_t resume_offset)
|
||||||
|
{
|
||||||
|
char *filename = NULL;
|
||||||
|
int flags;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (cur_resume)
|
||||||
|
flags = O_RDWR;
|
||||||
|
else
|
||||||
|
flags = O_WRONLY | O_TRUNC;
|
||||||
|
|
||||||
|
if (!cur_resume && !output_file)
|
||||||
|
flags |= O_EXCL;
|
||||||
|
|
||||||
|
flags |= O_CREAT;
|
||||||
|
|
||||||
|
if (output_file) {
|
||||||
|
if (!strcmp(output_file, "-")) {
|
||||||
|
if (!quiet)
|
||||||
|
fprintf(stderr, "Writing to stdout\n");
|
||||||
|
|
||||||
|
ret = STDOUT_FILENO;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
filename = uclient_get_url_filename(path, "index.html");
|
||||||
|
output_file = filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!quiet)
|
||||||
|
fprintf(stderr, "Writing to '%s'\n", output_file);
|
||||||
|
ret = open(output_file, flags, 0644);
|
||||||
|
if (ret < 0)
|
||||||
|
goto free;
|
||||||
|
|
||||||
|
if (resume_offset &&
|
||||||
|
lseek(ret, resume_offset, SEEK_SET) < 0) {
|
||||||
|
if (!quiet)
|
||||||
|
fprintf(stderr, "Failed to seek %"PRIu64" bytes in output file\n", resume_offset);
|
||||||
|
close(ret);
|
||||||
|
ret = -1;
|
||||||
|
goto free;
|
||||||
|
}
|
||||||
|
|
||||||
|
out_offset = resume_offset;
|
||||||
|
out_bytes += resume_offset;
|
||||||
|
done:
|
||||||
|
if (!quiet) {
|
||||||
|
progress_init(&pmt, output_file);
|
||||||
|
pmt_timer.cb = pmt_update;
|
||||||
|
pmt_timer.cb(&pmt_timer);
|
||||||
|
}
|
||||||
|
|
||||||
|
free:
|
||||||
|
free(filename);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void header_done_cb(struct uclient *cl)
|
||||||
|
{
|
||||||
|
enum {
|
||||||
|
H_RANGE,
|
||||||
|
H_LEN,
|
||||||
|
__H_MAX
|
||||||
|
};
|
||||||
|
static const struct blobmsg_policy policy[__H_MAX] = {
|
||||||
|
[H_RANGE] = { .name = "content-range", .type = BLOBMSG_TYPE_STRING },
|
||||||
|
[H_LEN] = { .name = "content-length", .type = BLOBMSG_TYPE_STRING },
|
||||||
|
};
|
||||||
|
struct blob_attr *tb[__H_MAX];
|
||||||
|
uint64_t resume_offset = 0, resume_end, resume_size;
|
||||||
|
static int retries;
|
||||||
|
|
||||||
|
if (retries < 10) {
|
||||||
|
int ret = uclient_http_redirect(cl);
|
||||||
|
if (ret < 0) {
|
||||||
|
if (!quiet)
|
||||||
|
fprintf(stderr, "Failed to redirect to %s on %s\n", cl->url->location, cl->url->host);
|
||||||
|
error_ret = 8;
|
||||||
|
request_done(cl);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (ret > 0) {
|
||||||
|
if (!quiet)
|
||||||
|
fprintf(stderr, "Redirected to %s on %s\n", cl->url->location, cl->url->host);
|
||||||
|
|
||||||
|
retries++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cl->status_code == 204 && cur_resume) {
|
||||||
|
/* Resume attempt failed, try normal download */
|
||||||
|
cur_resume = false;
|
||||||
|
init_request(cl);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
blobmsg_parse(policy, __H_MAX, tb, blob_data(cl->meta), blob_len(cl->meta));
|
||||||
|
|
||||||
|
switch (cl->status_code) {
|
||||||
|
case 416:
|
||||||
|
if (!quiet)
|
||||||
|
fprintf(stderr, "File download already fully retrieved; nothing to do.\n");
|
||||||
|
request_done(cl);
|
||||||
|
break;
|
||||||
|
case 206:
|
||||||
|
if (!cur_resume) {
|
||||||
|
if (!quiet)
|
||||||
|
fprintf(stderr, "Error: Partial content received, full content requested\n");
|
||||||
|
error_ret = 8;
|
||||||
|
request_done(cl);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tb[H_RANGE]) {
|
||||||
|
if (!quiet)
|
||||||
|
fprintf(stderr, "Content-Range header is missing\n");
|
||||||
|
error_ret = 8;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sscanf(blobmsg_get_string(tb[H_RANGE]),
|
||||||
|
"bytes %"PRIu64"-%"PRIu64"/%"PRIu64,
|
||||||
|
&resume_offset, &resume_end, &resume_size) != 3) {
|
||||||
|
if (!quiet)
|
||||||
|
fprintf(stderr, "Content-Range header is invalid\n");
|
||||||
|
error_ret = 8;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 204:
|
||||||
|
case 200:
|
||||||
|
if (no_output)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (tb[H_LEN])
|
||||||
|
out_len = strtoul(blobmsg_get_string(tb[H_LEN]), NULL, 10);
|
||||||
|
|
||||||
|
output_fd = open_output_file(cl->url->location, resume_offset);
|
||||||
|
if (output_fd < 0) {
|
||||||
|
if (!quiet)
|
||||||
|
perror("Cannot open output file");
|
||||||
|
error_ret = 3;
|
||||||
|
request_done(cl);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
if (!quiet)
|
||||||
|
fprintf(stderr, "HTTP error %d\n", cl->status_code);
|
||||||
|
request_done(cl);
|
||||||
|
error_ret = 8;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void read_data_cb(struct uclient *cl)
|
||||||
|
{
|
||||||
|
char buf[256];
|
||||||
|
ssize_t n;
|
||||||
|
int len;
|
||||||
|
|
||||||
|
if (!no_output && output_fd < 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
len = uclient_read(cl, buf, sizeof(buf));
|
||||||
|
if (!len)
|
||||||
|
return;
|
||||||
|
|
||||||
|
out_bytes += len;
|
||||||
|
if (!no_output) {
|
||||||
|
n = write(output_fd, buf, len);
|
||||||
|
if (n < 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void msg_connecting(struct uclient *cl)
|
||||||
|
{
|
||||||
|
char addr[INET6_ADDRSTRLEN];
|
||||||
|
int port;
|
||||||
|
|
||||||
|
if (quiet)
|
||||||
|
return;
|
||||||
|
|
||||||
|
uclient_get_addr(addr, &port, &cl->remote_addr);
|
||||||
|
fprintf(stderr, "Connecting to %s:%d\n", addr, port);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void check_resume_offset(struct uclient *cl)
|
||||||
|
{
|
||||||
|
char range_str[64];
|
||||||
|
struct stat st;
|
||||||
|
char *file;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
file = uclient_get_url_filename(cl->url->location, "index.html");
|
||||||
|
if (!file)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ret = stat(file, &st);
|
||||||
|
free(file);
|
||||||
|
if (ret)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!st.st_size)
|
||||||
|
return;
|
||||||
|
|
||||||
|
snprintf(range_str, sizeof(range_str), "bytes=%"PRIu64"-", (uint64_t) st.st_size);
|
||||||
|
uclient_http_set_header(cl, "Range", range_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int init_request(struct uclient *cl)
|
||||||
|
{
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
out_offset = 0;
|
||||||
|
out_bytes = 0;
|
||||||
|
out_len = 0;
|
||||||
|
uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
|
||||||
|
|
||||||
|
if (timeout)
|
||||||
|
cl->timeout_msecs = timeout * 1000;
|
||||||
|
|
||||||
|
rc = uclient_connect(cl);
|
||||||
|
if (rc)
|
||||||
|
return rc;
|
||||||
|
|
||||||
|
msg_connecting(cl);
|
||||||
|
|
||||||
|
rc = uclient_http_set_request_type(cl, post_data ? "POST" : "GET");
|
||||||
|
if (rc)
|
||||||
|
return rc;
|
||||||
|
|
||||||
|
uclient_http_reset_headers(cl);
|
||||||
|
uclient_http_set_header(cl, "User-Agent", user_agent);
|
||||||
|
if (cur_resume)
|
||||||
|
check_resume_offset(cl);
|
||||||
|
|
||||||
|
if (post_data) {
|
||||||
|
uclient_http_set_header(cl, "Content-Type", "application/x-www-form-urlencoded");
|
||||||
|
uclient_write(cl, post_data, strlen(post_data));
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = uclient_request(cl);
|
||||||
|
if (rc)
|
||||||
|
return rc;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void request_done(struct uclient *cl)
|
||||||
|
{
|
||||||
|
const char *proxy_url;
|
||||||
|
|
||||||
|
if (n_urls) {
|
||||||
|
proxy_url = get_proxy_url(*urls);
|
||||||
|
if (proxy_url) {
|
||||||
|
uclient_set_url(cl, proxy_url, NULL);
|
||||||
|
uclient_set_proxy_url(cl, *urls, auth_str);
|
||||||
|
} else {
|
||||||
|
uclient_set_url(cl, *urls, auth_str);
|
||||||
|
}
|
||||||
|
n_urls--;
|
||||||
|
cur_resume = resume;
|
||||||
|
error_ret = init_request(cl);
|
||||||
|
if (error_ret == 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (output_fd >= 0 && !output_file) {
|
||||||
|
close(output_fd);
|
||||||
|
output_fd = -1;
|
||||||
|
}
|
||||||
|
uclient_disconnect(cl);
|
||||||
|
uloop_end();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void eof_cb(struct uclient *cl)
|
||||||
|
{
|
||||||
|
if (!quiet) {
|
||||||
|
pmt_update(&pmt_timer);
|
||||||
|
uloop_timeout_cancel(&pmt_timer);
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cl->data_eof) {
|
||||||
|
if (!quiet)
|
||||||
|
fprintf(stderr, "Connection reset prematurely\n");
|
||||||
|
error_ret = 4;
|
||||||
|
} else if (!quiet) {
|
||||||
|
fprintf(stderr, "Download completed (%"PRIu64" bytes)\n", (uint64_t) out_bytes);
|
||||||
|
}
|
||||||
|
request_done(cl);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handle_uclient_error(struct uclient *cl, int code)
|
||||||
|
{
|
||||||
|
const char *type = "Unknown error";
|
||||||
|
bool ignore = false;
|
||||||
|
|
||||||
|
switch(code) {
|
||||||
|
case UCLIENT_ERROR_CONNECT:
|
||||||
|
type = "Connection failed";
|
||||||
|
error_ret = 4;
|
||||||
|
break;
|
||||||
|
case UCLIENT_ERROR_TIMEDOUT:
|
||||||
|
type = "Connection timed out";
|
||||||
|
error_ret = 4;
|
||||||
|
break;
|
||||||
|
case UCLIENT_ERROR_SSL_INVALID_CERT:
|
||||||
|
type = "Invalid SSL certificate";
|
||||||
|
ignore = !verify;
|
||||||
|
error_ret = 5;
|
||||||
|
break;
|
||||||
|
case UCLIENT_ERROR_SSL_CN_MISMATCH:
|
||||||
|
type = "Server hostname does not match SSL certificate";
|
||||||
|
ignore = !verify;
|
||||||
|
error_ret = 5;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
error_ret = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!quiet)
|
||||||
|
fprintf(stderr, "Connection error: %s%s\n", type, ignore ? " (ignored)" : "");
|
||||||
|
|
||||||
|
if (ignore)
|
||||||
|
error_ret = 0;
|
||||||
|
else
|
||||||
|
request_done(cl);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct uclient_cb cb = {
|
||||||
|
.header_done = header_done_cb,
|
||||||
|
.data_read = read_data_cb,
|
||||||
|
.data_eof = eof_cb,
|
||||||
|
.error = handle_uclient_error,
|
||||||
|
};
|
||||||
|
|
||||||
|
static int usage(const char *progname)
|
||||||
|
{
|
||||||
|
fprintf(stderr,
|
||||||
|
"Usage: %s [options] <URL>\n"
|
||||||
|
"Options:\n"
|
||||||
|
" -4 Use IPv4 only\n"
|
||||||
|
" -6 Use IPv6 only\n"
|
||||||
|
" -q Turn off status messages\n"
|
||||||
|
" -O <file> Redirect output to file (use \"-\" for stdout)\n"
|
||||||
|
" -P <dir> Set directory for output files\n"
|
||||||
|
" --user=<user> HTTP authentication username\n"
|
||||||
|
" --password=<password> HTTP authentication password\n"
|
||||||
|
" --user-agent|-U <str> Set HTTP user agent\n"
|
||||||
|
" --post-data=STRING use the POST method; send STRING as the data\n"
|
||||||
|
" --spider|-s Spider mode - only check file existence\n"
|
||||||
|
" --timeout=N|-T N Set connect/request timeout to N seconds\n"
|
||||||
|
" --proxy=on|off|-Y on|off Enable/disable env var configured proxy\n"
|
||||||
|
"\n"
|
||||||
|
"HTTPS options:\n"
|
||||||
|
" --ca-certificate=<cert> Load CA certificates from file <cert>\n"
|
||||||
|
" --no-check-certificate don't validate the server's certificate\n"
|
||||||
|
"\n", progname);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void init_ca_cert(void)
|
||||||
|
{
|
||||||
|
glob_t gl;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
glob("/etc/ssl/certs/*.crt", 0, NULL, &gl);
|
||||||
|
for (i = 0; i < gl.gl_pathc; i++)
|
||||||
|
ssl_ops->context_add_ca_crt_file(ssl_ctx, gl.gl_pathv[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void init_ustream_ssl(void)
|
||||||
|
{
|
||||||
|
void *dlh;
|
||||||
|
|
||||||
|
dlh = dlopen("libustream-ssl." LIB_EXT, RTLD_LAZY | RTLD_LOCAL);
|
||||||
|
if (!dlh)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ssl_ops = dlsym(dlh, "ustream_ssl_ops");
|
||||||
|
if (!ssl_ops)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ssl_ctx = ssl_ops->context_new(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int no_ssl(const char *progname)
|
||||||
|
{
|
||||||
|
fprintf(stderr,
|
||||||
|
"%s: SSL support not available, please install one of the "
|
||||||
|
"libustream-ssl-* libraries as well as the ca-bundle and "
|
||||||
|
"ca-certificates packages.\n",
|
||||||
|
progname);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum {
|
||||||
|
L_NO_CHECK_CERTIFICATE,
|
||||||
|
L_CA_CERTIFICATE,
|
||||||
|
L_USER,
|
||||||
|
L_PASSWORD,
|
||||||
|
L_USER_AGENT,
|
||||||
|
L_POST_DATA,
|
||||||
|
L_SPIDER,
|
||||||
|
L_TIMEOUT,
|
||||||
|
L_CONTINUE,
|
||||||
|
L_PROXY,
|
||||||
|
L_NO_PROXY,
|
||||||
|
L_QUIET,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct option longopts[] = {
|
||||||
|
[L_NO_CHECK_CERTIFICATE] = { "no-check-certificate", no_argument },
|
||||||
|
[L_CA_CERTIFICATE] = { "ca-certificate", required_argument },
|
||||||
|
[L_USER] = { "user", required_argument },
|
||||||
|
[L_PASSWORD] = { "password", required_argument },
|
||||||
|
[L_USER_AGENT] = { "user-agent", required_argument },
|
||||||
|
[L_POST_DATA] = { "post-data", required_argument },
|
||||||
|
[L_SPIDER] = { "spider", no_argument },
|
||||||
|
[L_TIMEOUT] = { "timeout", required_argument },
|
||||||
|
[L_CONTINUE] = { "continue", no_argument },
|
||||||
|
[L_PROXY] = { "proxy", required_argument },
|
||||||
|
[L_NO_PROXY] = { "no-proxy", no_argument },
|
||||||
|
[L_QUIET] = { "quiet", no_argument },
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
const char *progname = argv[0];
|
||||||
|
const char *proxy_url;
|
||||||
|
char *username = NULL;
|
||||||
|
char *password = NULL;
|
||||||
|
struct uclient *cl;
|
||||||
|
int longopt_idx = 0;
|
||||||
|
bool has_cert = false;
|
||||||
|
int i, ch;
|
||||||
|
int rc;
|
||||||
|
int af = -1;
|
||||||
|
|
||||||
|
signal(SIGPIPE, SIG_IGN);
|
||||||
|
init_ustream_ssl();
|
||||||
|
|
||||||
|
while ((ch = getopt_long(argc, argv, "46cO:P:qsT:U:Y:", longopts, &longopt_idx)) != -1) {
|
||||||
|
switch(ch) {
|
||||||
|
case 0:
|
||||||
|
switch (longopt_idx) {
|
||||||
|
case L_NO_CHECK_CERTIFICATE:
|
||||||
|
verify = false;
|
||||||
|
break;
|
||||||
|
case L_CA_CERTIFICATE:
|
||||||
|
has_cert = true;
|
||||||
|
if (ssl_ctx)
|
||||||
|
ssl_ops->context_add_ca_crt_file(ssl_ctx, optarg);
|
||||||
|
break;
|
||||||
|
case L_USER:
|
||||||
|
if (!strlen(optarg))
|
||||||
|
break;
|
||||||
|
username = strdup(optarg);
|
||||||
|
memset(optarg, '*', strlen(optarg));
|
||||||
|
break;
|
||||||
|
case L_PASSWORD:
|
||||||
|
if (!strlen(optarg))
|
||||||
|
break;
|
||||||
|
password = strdup(optarg);
|
||||||
|
memset(optarg, '*', strlen(optarg));
|
||||||
|
break;
|
||||||
|
case L_USER_AGENT:
|
||||||
|
user_agent = optarg;
|
||||||
|
break;
|
||||||
|
case L_POST_DATA:
|
||||||
|
post_data = optarg;
|
||||||
|
break;
|
||||||
|
case L_SPIDER:
|
||||||
|
no_output = true;
|
||||||
|
break;
|
||||||
|
case L_TIMEOUT:
|
||||||
|
timeout = atoi(optarg);
|
||||||
|
break;
|
||||||
|
case L_CONTINUE:
|
||||||
|
resume = true;
|
||||||
|
break;
|
||||||
|
case L_PROXY:
|
||||||
|
if (strcmp(optarg, "on") != 0)
|
||||||
|
proxy = false;
|
||||||
|
break;
|
||||||
|
case L_NO_PROXY:
|
||||||
|
proxy = false;
|
||||||
|
break;
|
||||||
|
case L_QUIET:
|
||||||
|
quiet = true;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return usage(progname);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '4':
|
||||||
|
af = AF_INET;
|
||||||
|
break;
|
||||||
|
case '6':
|
||||||
|
af = AF_INET6;
|
||||||
|
break;
|
||||||
|
case 'c':
|
||||||
|
resume = true;
|
||||||
|
break;
|
||||||
|
case 'U':
|
||||||
|
user_agent = optarg;
|
||||||
|
break;
|
||||||
|
case 'O':
|
||||||
|
output_file = optarg;
|
||||||
|
break;
|
||||||
|
case 'P':
|
||||||
|
if (chdir(optarg)) {
|
||||||
|
if (!quiet)
|
||||||
|
perror("Change output directory");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'q':
|
||||||
|
quiet = true;
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
no_output = true;
|
||||||
|
break;
|
||||||
|
case 'T':
|
||||||
|
timeout = atoi(optarg);
|
||||||
|
break;
|
||||||
|
case 'Y':
|
||||||
|
if (strcmp(optarg, "on") != 0)
|
||||||
|
proxy = false;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return usage(progname);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
argv += optind;
|
||||||
|
argc -= optind;
|
||||||
|
|
||||||
|
if (verify && !has_cert)
|
||||||
|
default_certs = true;
|
||||||
|
|
||||||
|
if (argc < 1)
|
||||||
|
return usage(progname);
|
||||||
|
|
||||||
|
if (!ssl_ctx) {
|
||||||
|
for (i = 0; i < argc; i++) {
|
||||||
|
if (!strncmp(argv[i], "https", 5))
|
||||||
|
return no_ssl(progname);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
urls = argv + 1;
|
||||||
|
n_urls = argc - 1;
|
||||||
|
|
||||||
|
uloop_init();
|
||||||
|
|
||||||
|
if (username) {
|
||||||
|
if (password) {
|
||||||
|
rc = asprintf(&auth_str, "%s:%s", username, password);
|
||||||
|
if (rc < 0)
|
||||||
|
return rc;
|
||||||
|
} else
|
||||||
|
auth_str = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!quiet)
|
||||||
|
fprintf(stderr, "Downloading '%s'\n", argv[0]);
|
||||||
|
|
||||||
|
proxy_url = get_proxy_url(argv[0]);
|
||||||
|
if (proxy_url) {
|
||||||
|
cl = uclient_new(proxy_url, auth_str, &cb);
|
||||||
|
if (cl)
|
||||||
|
uclient_set_proxy_url(cl, argv[0], NULL);
|
||||||
|
} else {
|
||||||
|
cl = uclient_new(argv[0], auth_str, &cb);
|
||||||
|
}
|
||||||
|
if (!cl) {
|
||||||
|
fprintf(stderr, "Failed to allocate uclient context\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (af >= 0)
|
||||||
|
uclient_http_set_address_family(cl, af);
|
||||||
|
|
||||||
|
if (ssl_ctx && default_certs)
|
||||||
|
init_ca_cert();
|
||||||
|
|
||||||
|
cur_resume = resume;
|
||||||
|
rc = init_request(cl);
|
||||||
|
if (!rc) {
|
||||||
|
/* no error received, we can enter main loop */
|
||||||
|
uloop_run();
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Failed to establish connection\n");
|
||||||
|
error_ret = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
uloop_done();
|
||||||
|
|
||||||
|
uclient_free(cl);
|
||||||
|
|
||||||
|
if (output_fd >= 0 && output_fd != STDOUT_FILENO)
|
||||||
|
close(output_fd);
|
||||||
|
|
||||||
|
if (ssl_ctx)
|
||||||
|
ssl_ops->context_free(ssl_ctx);
|
||||||
|
|
||||||
|
return error_ret;
|
||||||
|
}
|
||||||
1189
src/3P/uclient/uclient-http.c
Normal file
1189
src/3P/uclient/uclient-http.c
Normal file
File diff suppressed because it is too large
Load Diff
184
src/3P/uclient/uclient-utils.c
Normal file
184
src/3P/uclient/uclient-utils.c
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
/*
|
||||||
|
* uclient - ustream based protocol client library
|
||||||
|
*
|
||||||
|
* Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
#include <libubox/md5.h>
|
||||||
|
#include <libubox/utils.h>
|
||||||
|
|
||||||
|
#include "uclient-utils.h"
|
||||||
|
|
||||||
|
static const char *b64 =
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
|
||||||
|
void base64_encode(const void *inbuf, unsigned int len, void *outbuf)
|
||||||
|
{
|
||||||
|
unsigned char *out = outbuf;
|
||||||
|
const uint8_t *in = inbuf;
|
||||||
|
unsigned int i;
|
||||||
|
int pad = len % 3;
|
||||||
|
|
||||||
|
for (i = 0; i < len - pad; i += 3) {
|
||||||
|
uint32_t in3 = (in[0] << 16) | (in[1] << 8) | in[2];
|
||||||
|
int k;
|
||||||
|
|
||||||
|
for (k = 3; k >= 0; k--) {
|
||||||
|
out[k] = b64[in3 & 0x3f];
|
||||||
|
in3 >>= 6;
|
||||||
|
}
|
||||||
|
in += 3;
|
||||||
|
out += 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pad) {
|
||||||
|
uint32_t in2 = in[0] << (16 - 6);
|
||||||
|
|
||||||
|
out[3] = '=';
|
||||||
|
|
||||||
|
if (pad > 1) {
|
||||||
|
in2 |= in[1] << (8 - 6);
|
||||||
|
out[2] = b64[in2 & 0x3f];
|
||||||
|
} else {
|
||||||
|
out[2] = '=';
|
||||||
|
}
|
||||||
|
|
||||||
|
in2 >>= 6;
|
||||||
|
out[1] = b64[in2 & 0x3f];
|
||||||
|
in2 >>= 6;
|
||||||
|
out[0] = b64[in2 & 0x3f];
|
||||||
|
|
||||||
|
out += 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
int uclient_urldecode(const char *in, char *out, bool decode_plus)
|
||||||
|
{
|
||||||
|
static char dec[3];
|
||||||
|
int ret = 0;
|
||||||
|
char c;
|
||||||
|
|
||||||
|
while ((c = *(in++))) {
|
||||||
|
if (c == '%') {
|
||||||
|
if (!isxdigit(in[0]) || !isxdigit(in[1]))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
dec[0] = in[0];
|
||||||
|
dec[1] = in[1];
|
||||||
|
c = strtol(dec, NULL, 16);
|
||||||
|
in += 2;
|
||||||
|
} else if (decode_plus && c == '+') {
|
||||||
|
c = ' ';
|
||||||
|
}
|
||||||
|
|
||||||
|
*(out++) = c;
|
||||||
|
ret++;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = 0;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char hex_digit(char val)
|
||||||
|
{
|
||||||
|
val += val > 9 ? 'a' - 10 : '0';
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
void bin_to_hex(char *dest, const void *buf, int len)
|
||||||
|
{
|
||||||
|
const uint8_t *data = buf;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
*(dest++) = hex_digit(data[i] >> 4);
|
||||||
|
*(dest++) = hex_digit(data[i] & 0xf);
|
||||||
|
}
|
||||||
|
*dest = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void http_create_hash(char *dest, const char * const * str, int n_str)
|
||||||
|
{
|
||||||
|
uint32_t hash[4];
|
||||||
|
md5_ctx_t md5;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
md5_begin(&md5);
|
||||||
|
for (i = 0; i < n_str; i++) {
|
||||||
|
if (i)
|
||||||
|
md5_hash(":", 1, &md5);
|
||||||
|
md5_hash(str[i], strlen(str[i]), &md5);
|
||||||
|
}
|
||||||
|
md5_end(hash, &md5);
|
||||||
|
bin_to_hex(dest, &hash, sizeof(hash));
|
||||||
|
}
|
||||||
|
|
||||||
|
void http_digest_calculate_auth_hash(char *dest, const char *user, const char *realm, const char *password)
|
||||||
|
{
|
||||||
|
const char *hash_str[] = {
|
||||||
|
user,
|
||||||
|
realm,
|
||||||
|
password
|
||||||
|
};
|
||||||
|
|
||||||
|
http_create_hash(dest, hash_str, ARRAY_SIZE(hash_str));
|
||||||
|
}
|
||||||
|
|
||||||
|
void http_digest_calculate_response(char *dest, const struct http_digest_data *data)
|
||||||
|
{
|
||||||
|
const char *h_a2_strings[] = {
|
||||||
|
data->method,
|
||||||
|
data->uri,
|
||||||
|
};
|
||||||
|
const char *resp_strings[] = {
|
||||||
|
data->auth_hash,
|
||||||
|
data->nonce,
|
||||||
|
data->nc,
|
||||||
|
data->cnonce,
|
||||||
|
data->qop,
|
||||||
|
dest, /* initialized to H(A2) first */
|
||||||
|
};
|
||||||
|
|
||||||
|
http_create_hash(dest, h_a2_strings, ARRAY_SIZE(h_a2_strings));
|
||||||
|
http_create_hash(dest, resp_strings, ARRAY_SIZE(resp_strings));
|
||||||
|
}
|
||||||
|
|
||||||
|
char *uclient_get_url_filename(const char *url, const char *default_name)
|
||||||
|
{
|
||||||
|
const char *str;
|
||||||
|
int len = strcspn(url, ";&");
|
||||||
|
|
||||||
|
while (len > 0 && url[len - 1] == '/')
|
||||||
|
len--;
|
||||||
|
|
||||||
|
for (str = url + len - 1; str >= url; str--) {
|
||||||
|
if (*str == '/')
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
str++;
|
||||||
|
len -= str - url;
|
||||||
|
|
||||||
|
if (len > 0)
|
||||||
|
return strncpy(calloc(1, len + 1), str, len);
|
||||||
|
|
||||||
|
return strdup(default_name);
|
||||||
|
}
|
||||||
49
src/3P/uclient/uclient-utils.h
Normal file
49
src/3P/uclient/uclient-utils.h
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* uclient - ustream based protocol client library
|
||||||
|
*
|
||||||
|
* Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
#ifndef __UCLIENT_UTILS_H
|
||||||
|
#define __UCLIENT_UTILS_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
struct http_digest_data {
|
||||||
|
const char *uri;
|
||||||
|
const char *method;
|
||||||
|
|
||||||
|
const char *auth_hash; /* H(A1) */
|
||||||
|
const char *qop;
|
||||||
|
const char *nc;
|
||||||
|
const char *nonce;
|
||||||
|
const char *cnonce;
|
||||||
|
};
|
||||||
|
|
||||||
|
static inline int base64_len(int len)
|
||||||
|
{
|
||||||
|
return ((len + 2) / 3) * 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
void base64_encode(const void *inbuf, unsigned int len, void *out);
|
||||||
|
void bin_to_hex(char *dest, const void *buf, int len);
|
||||||
|
|
||||||
|
int uclient_urldecode(const char *in, char *out, bool decode_plus);
|
||||||
|
|
||||||
|
void http_digest_calculate_auth_hash(char *dest, const char *user, const char *realm, const char *password);
|
||||||
|
void http_digest_calculate_response(char *dest, const struct http_digest_data *data);
|
||||||
|
|
||||||
|
char *uclient_get_url_filename(const char *url, const char *default_name);
|
||||||
|
|
||||||
|
#endif
|
||||||
421
src/3P/uclient/uclient.c
Normal file
421
src/3P/uclient/uclient.c
Normal file
@@ -0,0 +1,421 @@
|
|||||||
|
/*
|
||||||
|
* uclient - ustream based protocol client library
|
||||||
|
*
|
||||||
|
* Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <libubox/ustream-ssl.h>
|
||||||
|
#include "uclient.h"
|
||||||
|
#include "uclient-utils.h"
|
||||||
|
#include "uclient-backend.h"
|
||||||
|
|
||||||
|
char *uclient_get_addr(char *dest, int *port, union uclient_addr *a)
|
||||||
|
{
|
||||||
|
int portval;
|
||||||
|
void *ptr;
|
||||||
|
|
||||||
|
switch(a->sa.sa_family) {
|
||||||
|
case AF_INET:
|
||||||
|
ptr = &a->sin.sin_addr;
|
||||||
|
portval = a->sin.sin_port;
|
||||||
|
break;
|
||||||
|
case AF_INET6:
|
||||||
|
ptr = &a->sin6.sin6_addr;
|
||||||
|
portval = a->sin6.sin6_port;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return strcpy(dest, "Unknown");
|
||||||
|
}
|
||||||
|
|
||||||
|
inet_ntop(a->sa.sa_family, ptr, dest, INET6_ADDRSTRLEN);
|
||||||
|
if (port)
|
||||||
|
*port = ntohs(portval);
|
||||||
|
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct uclient_url *
|
||||||
|
__uclient_get_url(const struct uclient_backend *backend,
|
||||||
|
const char *host, int host_len,
|
||||||
|
const char *location, const char *auth_str)
|
||||||
|
{
|
||||||
|
struct uclient_url *url;
|
||||||
|
char *host_buf, *uri_buf, *auth_buf, *next;
|
||||||
|
|
||||||
|
url = calloc_a(sizeof(*url),
|
||||||
|
&host_buf, host_len + 1,
|
||||||
|
&uri_buf, strlen(location) + 1,
|
||||||
|
&auth_buf, auth_str ? strlen(auth_str) + 1 : 0);
|
||||||
|
|
||||||
|
url->backend = backend;
|
||||||
|
url->location = strcpy(uri_buf, location);
|
||||||
|
if (host)
|
||||||
|
url->host = strncpy(host_buf, host, host_len);
|
||||||
|
|
||||||
|
next = strchr(host_buf, '@');
|
||||||
|
if (next) {
|
||||||
|
*next = 0;
|
||||||
|
url->host = next + 1;
|
||||||
|
|
||||||
|
if (uclient_urldecode(host_buf, host_buf, false) < 0)
|
||||||
|
goto free;
|
||||||
|
|
||||||
|
url->auth = host_buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!url->auth && auth_str)
|
||||||
|
url->auth = strcpy(auth_buf, auth_str);
|
||||||
|
|
||||||
|
/* Literal IPv6 address */
|
||||||
|
if (*url->host == '[') {
|
||||||
|
url->host++;
|
||||||
|
next = strrchr(url->host, ']');
|
||||||
|
if (!next)
|
||||||
|
goto free;
|
||||||
|
|
||||||
|
*(next++) = 0;
|
||||||
|
if (*next == ':')
|
||||||
|
url->port = next + 1;
|
||||||
|
} else {
|
||||||
|
next = strrchr(url->host, ':');
|
||||||
|
if (next) {
|
||||||
|
*next = 0;
|
||||||
|
url->port = next + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return url;
|
||||||
|
|
||||||
|
free:
|
||||||
|
free(url);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *
|
||||||
|
uclient_split_host(const char *base, int *host_len)
|
||||||
|
{
|
||||||
|
char *next, *location;
|
||||||
|
|
||||||
|
next = strchr(base, '/');
|
||||||
|
if (next) {
|
||||||
|
location = next;
|
||||||
|
*host_len = next - base;
|
||||||
|
} else {
|
||||||
|
location = "/";
|
||||||
|
*host_len = strlen(base);
|
||||||
|
}
|
||||||
|
|
||||||
|
return location;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct uclient_url __hidden *
|
||||||
|
uclient_get_url_location(struct uclient_url *url, const char *location)
|
||||||
|
{
|
||||||
|
struct uclient_url *new_url;
|
||||||
|
char *host_buf, *uri_buf, *auth_buf, *port_buf;
|
||||||
|
int host_len = strlen(url->host) + 1;
|
||||||
|
int auth_len = url->auth ? strlen(url->auth) + 1 : 0;
|
||||||
|
int port_len = url->port ? strlen(url->port) + 1 : 0;
|
||||||
|
int uri_len;
|
||||||
|
|
||||||
|
if (strstr(location, "://"))
|
||||||
|
return uclient_get_url(location, url->auth);
|
||||||
|
|
||||||
|
if (location[0] == '/')
|
||||||
|
uri_len = strlen(location) + 1;
|
||||||
|
else
|
||||||
|
uri_len = strlen(url->location) + strlen(location) + 2;
|
||||||
|
|
||||||
|
new_url = calloc_a(sizeof(*url),
|
||||||
|
&host_buf, host_len,
|
||||||
|
&port_buf, port_len,
|
||||||
|
&uri_buf, uri_len,
|
||||||
|
&auth_buf, auth_len);
|
||||||
|
|
||||||
|
if (!new_url)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
new_url->backend = url->backend;
|
||||||
|
new_url->prefix = url->prefix;
|
||||||
|
new_url->host = strcpy(host_buf, url->host);
|
||||||
|
if (url->port)
|
||||||
|
new_url->port = strcpy(port_buf, url->port);
|
||||||
|
if (url->auth)
|
||||||
|
new_url->auth = strcpy(auth_buf, url->auth);
|
||||||
|
|
||||||
|
new_url->location = uri_buf;
|
||||||
|
if (location[0] == '/')
|
||||||
|
strcpy(uri_buf, location);
|
||||||
|
else {
|
||||||
|
int len = strcspn(url->location, "?#");
|
||||||
|
char *buf = uri_buf;
|
||||||
|
|
||||||
|
memcpy(buf, url->location, len);
|
||||||
|
if (buf[len - 1] != '/') {
|
||||||
|
buf[len] = '/';
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
|
||||||
|
buf += len;
|
||||||
|
strcpy(buf, location);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new_url;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct uclient_url __hidden *
|
||||||
|
uclient_get_url(const char *url_str, const char *auth_str)
|
||||||
|
{
|
||||||
|
static const struct uclient_backend *backends[] = {
|
||||||
|
&uclient_backend_http,
|
||||||
|
};
|
||||||
|
|
||||||
|
const struct uclient_backend *backend;
|
||||||
|
const char * const *prefix = NULL;
|
||||||
|
struct uclient_url *url;
|
||||||
|
const char *location;
|
||||||
|
int host_len;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < ARRAY_SIZE(backends); i++) {
|
||||||
|
int prefix_len = 0;
|
||||||
|
|
||||||
|
for (prefix = backends[i]->prefix; *prefix; prefix++) {
|
||||||
|
prefix_len = strlen(*prefix);
|
||||||
|
|
||||||
|
if (!strncmp(url_str, *prefix, prefix_len))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!*prefix)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
url_str += prefix_len;
|
||||||
|
backend = backends[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!*prefix)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
location = uclient_split_host(url_str, &host_len);
|
||||||
|
url = __uclient_get_url(backend, url_str, host_len, location, auth_str);
|
||||||
|
if (!url)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
url->prefix = prefix - backend->prefix;
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void uclient_connection_timeout(struct uloop_timeout *timeout)
|
||||||
|
{
|
||||||
|
struct uclient *cl = container_of(timeout, struct uclient, connection_timeout);
|
||||||
|
|
||||||
|
if (cl->backend->disconnect)
|
||||||
|
cl->backend->disconnect(cl);
|
||||||
|
|
||||||
|
uclient_backend_set_error(cl, UCLIENT_ERROR_TIMEDOUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct uclient *uclient_new(const char *url_str, const char *auth_str, const struct uclient_cb *cb)
|
||||||
|
{
|
||||||
|
struct uclient *cl;
|
||||||
|
struct uclient_url *url;
|
||||||
|
|
||||||
|
url = uclient_get_url(url_str, auth_str);
|
||||||
|
if (!url)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
cl = url->backend->alloc();
|
||||||
|
if (!cl)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
cl->backend = url->backend;
|
||||||
|
cl->cb = cb;
|
||||||
|
cl->url = url;
|
||||||
|
cl->timeout_msecs = UCLIENT_DEFAULT_TIMEOUT_MS;
|
||||||
|
cl->connection_timeout.cb = uclient_connection_timeout;
|
||||||
|
|
||||||
|
return cl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int uclient_set_proxy_url(struct uclient *cl, const char *url_str, const char *auth_str)
|
||||||
|
{
|
||||||
|
const struct uclient_backend *backend = cl->backend;
|
||||||
|
struct uclient_url *url;
|
||||||
|
int host_len;
|
||||||
|
char *next, *host;
|
||||||
|
|
||||||
|
if (!backend->update_proxy_url)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
next = strstr(url_str, "://");
|
||||||
|
if (!next)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
host = next + 3;
|
||||||
|
uclient_split_host(host, &host_len);
|
||||||
|
|
||||||
|
url = __uclient_get_url(NULL, host, host_len, url_str, auth_str);
|
||||||
|
if (!url)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
free(cl->proxy_url);
|
||||||
|
cl->proxy_url = url;
|
||||||
|
|
||||||
|
if (backend->update_proxy_url)
|
||||||
|
backend->update_proxy_url(cl);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int uclient_set_url(struct uclient *cl, const char *url_str, const char *auth_str)
|
||||||
|
{
|
||||||
|
const struct uclient_backend *backend = cl->backend;
|
||||||
|
struct uclient_url *url = cl->url;
|
||||||
|
|
||||||
|
url = uclient_get_url(url_str, auth_str);
|
||||||
|
if (!url)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (url->backend != cl->backend) {
|
||||||
|
free(url);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(cl->proxy_url);
|
||||||
|
cl->proxy_url = NULL;
|
||||||
|
|
||||||
|
free(cl->url);
|
||||||
|
cl->url = url;
|
||||||
|
|
||||||
|
if (backend->update_url)
|
||||||
|
backend->update_url(cl);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int uclient_set_timeout(struct uclient *cl, int msecs)
|
||||||
|
{
|
||||||
|
if (msecs <= 0)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
cl->timeout_msecs = msecs;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int uclient_connect(struct uclient *cl)
|
||||||
|
{
|
||||||
|
return cl->backend->connect(cl);
|
||||||
|
}
|
||||||
|
|
||||||
|
void uclient_free(struct uclient *cl)
|
||||||
|
{
|
||||||
|
struct uclient_url *url = cl->url;
|
||||||
|
|
||||||
|
if (cl->backend->free)
|
||||||
|
cl->backend->free(cl);
|
||||||
|
else
|
||||||
|
free(cl);
|
||||||
|
|
||||||
|
free(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
int uclient_write(struct uclient *cl, const char *buf, int len)
|
||||||
|
{
|
||||||
|
if (!cl->backend->write)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return cl->backend->write(cl, buf, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
int uclient_request(struct uclient *cl)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if (!cl->backend->request)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
err = cl->backend->request(cl);
|
||||||
|
if (err)
|
||||||
|
return err;
|
||||||
|
|
||||||
|
uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int uclient_read(struct uclient *cl, char *buf, int len)
|
||||||
|
{
|
||||||
|
if (!cl->backend->read)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return cl->backend->read(cl, buf, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void uclient_disconnect(struct uclient *cl)
|
||||||
|
{
|
||||||
|
uloop_timeout_cancel(&cl->connection_timeout);
|
||||||
|
|
||||||
|
if (!cl->backend->disconnect)
|
||||||
|
return;
|
||||||
|
|
||||||
|
cl->backend->disconnect(cl);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void __uclient_backend_change_state(struct uloop_timeout *timeout)
|
||||||
|
{
|
||||||
|
struct uclient *cl = container_of(timeout, struct uclient, timeout);
|
||||||
|
|
||||||
|
if (cl->error_code && cl->cb->error)
|
||||||
|
cl->cb->error(cl, cl->error_code);
|
||||||
|
else if (cl->eof && cl->cb->data_eof)
|
||||||
|
cl->cb->data_eof(cl);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void uclient_backend_change_state(struct uclient *cl)
|
||||||
|
{
|
||||||
|
cl->timeout.cb = __uclient_backend_change_state;
|
||||||
|
uloop_timeout_set(&cl->timeout, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __hidden uclient_backend_set_error(struct uclient *cl, int code)
|
||||||
|
{
|
||||||
|
if (cl->error_code)
|
||||||
|
return;
|
||||||
|
|
||||||
|
uloop_timeout_cancel(&cl->connection_timeout);
|
||||||
|
cl->error_code = code;
|
||||||
|
uclient_backend_change_state(cl);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __hidden uclient_backend_set_eof(struct uclient *cl)
|
||||||
|
{
|
||||||
|
if (cl->eof || cl->error_code)
|
||||||
|
return;
|
||||||
|
|
||||||
|
uloop_timeout_cancel(&cl->connection_timeout);
|
||||||
|
cl->eof = true;
|
||||||
|
uclient_backend_change_state(cl);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __hidden uclient_backend_reset_state(struct uclient *cl)
|
||||||
|
{
|
||||||
|
cl->data_eof = false;
|
||||||
|
cl->eof = false;
|
||||||
|
cl->error_code = 0;
|
||||||
|
uloop_timeout_cancel(&cl->timeout);
|
||||||
|
}
|
||||||
130
src/3P/uclient/uclient.h
Normal file
130
src/3P/uclient/uclient.h
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
/*
|
||||||
|
* uclient - ustream based protocol client library
|
||||||
|
*
|
||||||
|
* Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
#ifndef __LIBUBOX_UCLIENT_H
|
||||||
|
#define __LIBUBOX_UCLIENT_H
|
||||||
|
|
||||||
|
#include <netinet/in.h>
|
||||||
|
|
||||||
|
#include <libubox/blob.h>
|
||||||
|
#include <libubox/ustream.h>
|
||||||
|
#include <libubox/ustream-ssl.h>
|
||||||
|
|
||||||
|
#define UCLIENT_DEFAULT_TIMEOUT_MS 30000
|
||||||
|
|
||||||
|
struct uclient_cb;
|
||||||
|
struct uclient_backend;
|
||||||
|
|
||||||
|
enum uclient_error_code {
|
||||||
|
UCLIENT_ERROR_UNKNOWN,
|
||||||
|
UCLIENT_ERROR_CONNECT,
|
||||||
|
UCLIENT_ERROR_TIMEDOUT,
|
||||||
|
UCLIENT_ERROR_SSL_INVALID_CERT,
|
||||||
|
UCLIENT_ERROR_SSL_CN_MISMATCH,
|
||||||
|
UCLIENT_ERROR_MISSING_SSL_CONTEXT,
|
||||||
|
};
|
||||||
|
|
||||||
|
union uclient_addr {
|
||||||
|
struct sockaddr sa;
|
||||||
|
struct sockaddr_in sin;
|
||||||
|
struct sockaddr_in6 sin6;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct uclient_url {
|
||||||
|
const struct uclient_backend *backend;
|
||||||
|
int prefix;
|
||||||
|
|
||||||
|
const char *host;
|
||||||
|
const char *port;
|
||||||
|
const char *location;
|
||||||
|
|
||||||
|
const char *auth;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct uclient {
|
||||||
|
const struct uclient_backend *backend;
|
||||||
|
const struct uclient_cb *cb;
|
||||||
|
|
||||||
|
union uclient_addr local_addr, remote_addr;
|
||||||
|
|
||||||
|
struct uclient_url *proxy_url;
|
||||||
|
struct uclient_url *url;
|
||||||
|
int timeout_msecs;
|
||||||
|
void *priv;
|
||||||
|
|
||||||
|
bool eof;
|
||||||
|
bool data_eof;
|
||||||
|
int error_code;
|
||||||
|
int status_code;
|
||||||
|
int seq;
|
||||||
|
struct blob_attr *meta;
|
||||||
|
|
||||||
|
struct uloop_timeout connection_timeout;
|
||||||
|
struct uloop_timeout timeout;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct uclient_cb {
|
||||||
|
void (*data_read)(struct uclient *cl);
|
||||||
|
void (*data_sent)(struct uclient *cl);
|
||||||
|
void (*data_eof)(struct uclient *cl);
|
||||||
|
void (*header_done)(struct uclient *cl);
|
||||||
|
void (*error)(struct uclient *cl, int code);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct uclient *uclient_new(const char *url, const char *auth_str, const struct uclient_cb *cb);
|
||||||
|
void uclient_free(struct uclient *cl);
|
||||||
|
|
||||||
|
int uclient_set_url(struct uclient *cl, const char *url, const char *auth);
|
||||||
|
int uclient_set_proxy_url(struct uclient *cl, const char *url_str, const char *auth_str);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets connection timeout.
|
||||||
|
*
|
||||||
|
* Provided timeout value will be used for:
|
||||||
|
* 1) Receiving HTTP response
|
||||||
|
* 2) Receiving data
|
||||||
|
*
|
||||||
|
* In case of timeout uclient will use error callback with
|
||||||
|
* UCLIENT_ERROR_TIMEDOUT code.
|
||||||
|
*
|
||||||
|
* @param msecs timeout in milliseconds
|
||||||
|
*/
|
||||||
|
int uclient_set_timeout(struct uclient *cl, int msecs);
|
||||||
|
|
||||||
|
int uclient_connect(struct uclient *cl);
|
||||||
|
void uclient_disconnect(struct uclient *cl);
|
||||||
|
|
||||||
|
int uclient_read(struct uclient *cl, char *buf, int len);
|
||||||
|
int uclient_write(struct uclient *cl, const char *buf, int len);
|
||||||
|
int uclient_request(struct uclient *cl);
|
||||||
|
|
||||||
|
char *uclient_get_addr(char *dest, int *port, union uclient_addr *a);
|
||||||
|
|
||||||
|
/* HTTP */
|
||||||
|
extern const struct uclient_backend uclient_backend_http;
|
||||||
|
|
||||||
|
int uclient_http_reset_headers(struct uclient *cl);
|
||||||
|
int uclient_http_set_header(struct uclient *cl, const char *name, const char *value);
|
||||||
|
int uclient_http_set_request_type(struct uclient *cl, const char *type);
|
||||||
|
int uclient_http_redirect(struct uclient *cl);
|
||||||
|
|
||||||
|
int uclient_http_set_ssl_ctx(struct uclient *cl, const struct ustream_ssl_ops *ops,
|
||||||
|
struct ustream_ssl_ctx *ctx, bool require_validation);
|
||||||
|
int uclient_http_set_address_family(struct uclient *cl, int af);
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user