Update networking layer w/ CURL and emscripten impl

This commit is contained in:
2025-11-08 01:50:36 +11:00
parent a17925904d
commit f6874dc55a
4105 changed files with 694617 additions and 179 deletions
+27
View File
@@ -0,0 +1,27 @@
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# SPDX-License-Identifier: curl
allowfunc fclose
allowfunc fdopen
allowfunc fopen
allowfunc fprintf
allowfunc gmtime
allowfunc localtime
allowfunc open
allowfunc printf
allowfunc snprintf
allowfunc socket
allowfunc sscanf
allowfunc strerror
allowfunc vsnprintf
# Use of curl printf functions is discouraged
banfunc curl_maprintf
banfunc curl_mfprintf
banfunc curl_mprintf
banfunc curl_msnprintf
banfunc curl_mvaprintf
banfunc curl_mvfprintf
banfunc curl_mvprintf
banfunc curl_mvsnprintf
+160
View File
@@ -0,0 +1,160 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Download many files in parallel, in the same thread.
* </DESC>
*/
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
static const char *urls[] = {
"https://www.microsoft.com",
"https://opensource.org",
"https://www.google.com",
"https://www.yahoo.com",
"https://www.ibm.com",
"https://www.mysql.com",
"https://www.oracle.com",
"https://www.ripe.net",
"https://www.iana.org",
"https://www.amazon.com",
"https://www.netcraft.com",
"https://www.heise.de",
"https://www.chip.de",
"https://www.ca.com",
"https://www.cnet.com",
"https://www.mozilla.org",
"https://www.cnn.com",
"https://www.wikipedia.org",
"https://www.dell.com",
"https://www.hp.com",
"https://www.cert.org",
"https://www.mit.edu",
"https://www.nist.gov",
"https://www.ebay.com",
"https://www.playstation.com",
"https://www.uefa.com",
"https://www.ieee.org",
"https://www.apple.com",
"https://www.symantec.com",
"https://www.zdnet.com",
"https://www.fujitsu.com/global/",
"https://www.supermicro.com",
"https://www.hotmail.com",
"https://www.ietf.org",
"https://www.bbc.co.uk",
"https://news.google.com",
"https://www.foxnews.com",
"https://www.msn.com",
"https://www.wired.com",
"https://www.sky.com",
"https://www.usatoday.com",
"https://www.cbs.com",
"https://www.nbc.com/",
"https://slashdot.org",
"https://www.informationweek.com",
"https://apache.org",
"https://www.un.org",
};
#define MAX_PARALLEL 10 /* number of simultaneous transfers */
#define NUM_URLS sizeof(urls)/sizeof(char *)
static size_t write_cb(char *data, size_t n, size_t l, void *userp)
{
/* take care of the data here, ignored in this example */
(void)data;
(void)userp;
return n * l;
}
static void add_transfer(CURLM *multi, unsigned int i, int *left)
{
CURL *curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(curl, CURLOPT_URL, urls[i]);
curl_easy_setopt(curl, CURLOPT_PRIVATE, urls[i]);
curl_multi_add_handle(multi, curl);
}
(*left)++;
}
int main(void)
{
CURLM *multi;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
multi = curl_multi_init();
if(multi) {
CURLMsg *msg;
unsigned int transfers = 0;
int msgs_left = -1;
int left = 0;
/* Limit the amount of simultaneous connections curl should allow: */
curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, (long)MAX_PARALLEL);
for(transfers = 0; transfers < MAX_PARALLEL && transfers < NUM_URLS;
transfers++)
add_transfer(multi, transfers, &left);
do {
int still_alive = 1;
curl_multi_perform(multi, &still_alive);
/* !checksrc! disable EQUALSNULL 1 */
while((msg = curl_multi_info_read(multi, &msgs_left)) != NULL) {
if(msg->msg == CURLMSG_DONE) {
char *url;
CURL *curl = msg->easy_handle;
curl_easy_getinfo(curl, CURLINFO_PRIVATE, &url);
fprintf(stderr, "R: %d - %s <%s>\n",
msg->data.result, curl_easy_strerror(msg->data.result), url);
curl_multi_remove_handle(multi, curl);
curl_easy_cleanup(curl);
left--;
}
else {
fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
}
if(transfers < NUM_URLS)
add_transfer(multi, transfers++, &left);
}
if(left)
curl_multi_wait(multi, NULL, 0, 1000, NULL);
} while(left);
curl_multi_cleanup(multi);
}
curl_global_cleanup();
return EXIT_SUCCESS;
}
+93
View File
@@ -0,0 +1,93 @@
#***************************************************************************
# _ _ ____ _
# Project ___| | | | _ \| |
# / __| | | | |_) | |
# | (__| |_| | _ <| |___
# \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################
add_custom_target(curl-examples)
# Get check_PROGRAMS, COMPLICATED_MAY_BUILD, COMPLICATED_EXAMPLES variables
curl_transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
set(_with_deps "")
set(_all_src "")
set(_all_canary "")
set(_all "all")
foreach(_target IN LISTS COMPLICATED_MAY_BUILD check_PROGRAMS _all) # keep 'COMPLICATED_MAY_BUILD' first, and '_all' last
# Strip .c suffix from COMPLICATED_MAY_BUILD items
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.20)
cmake_path(GET _target STEM _target)
else()
get_filename_component(_target "${_target}" NAME_WE)
endif()
set(_more_libs "")
set(_target_name "curl-example-${_target}")
if(_target STREQUAL "all")
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12)
add_library(${_target_name} OBJECT EXCLUDE_FROM_ALL ${_all_src})
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC") # MSVC but exclude clang-cl
# CMake generates a static library for the OBJECT target. Silence these 'lib.exe' warnings:
# warning LNK4006: main already defined in ....obj; second definition ignored
# warning LNK4221: This object file does not define any previously undefined public symbols,
# so it will not be used by any link operation that consumes this library
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.13)
set_target_properties(${_target_name} PROPERTIES STATIC_LIBRARY_OPTIONS "-ignore:4006;-ignore:4221")
else()
set_target_properties(${_target_name} PROPERTIES STATIC_LIBRARY_FLAGS "-ignore:4006 -ignore:4221")
endif()
endif()
else()
add_library(${_target_name} STATIC EXCLUDE_FROM_ALL ${_all_src})
endif()
if(_with_deps)
set(_more_libs ${CURL_LIBS}) # If any examples required dependencies, link them
endif()
add_custom_target(curl-examples-build) # Special target to compile all tests quickly and build a single test to probe linkage
add_dependencies(curl-examples-build ${_target_name} ${_all_canary}) # Include a full build of a single test
else()
# Check if the example requires a build option. Then check if that build option is enabled.
# If it is, link all dependencies to the example.
set(_requires_regex "/\\* Requires: ([A-Z0-9_]+) \\*/")
file(STRINGS "${_target}.c" _req REGEX "${_requires_regex}")
string(REGEX REPLACE "${_requires_regex}" "\\1" _req "${_req}")
if(_req)
if(${${_req}})
string(APPEND _with_deps " ${_target}:${_req}")
set(_more_libs ${CURL_LIBS})
else()
continue() # Option required, but not found
endif()
endif()
set(_all_canary ${_target_name}) # Save the last test for the curl-examples-build target
list(APPEND _all_src "${_target}.c")
add_executable(${_target_name} EXCLUDE_FROM_ALL "${_target}.c")
add_dependencies(curl-examples ${_target_name})
endif()
target_link_libraries(${_target_name} ${LIB_SELECTED} ${CURL_NETWORK_AND_TIME_LIBS} ${_more_libs})
target_compile_definitions(${_target_name} PRIVATE "CURL_NO_OLDIES"
"$<$<BOOL:${WIN32}>:WIN32_LEAN_AND_MEAN>" "$<$<BOOL:${MSVC}>:_CRT_SECURE_NO_DEPRECATE>")
set_target_properties(${_target_name} PROPERTIES OUTPUT_NAME "${_target}" PROJECT_LABEL "Example ${_target}" UNITY_BUILD OFF)
endforeach()
if(_with_deps)
message(STATUS "Enabled examples with dependencies:${_with_deps}")
endif()
+75
View File
@@ -0,0 +1,75 @@
#***************************************************************************
# _ _ ____ _
# Project ___| | | | _ \| |
# / __| | | | |_) | |
# | (__| |_| | _ <| |___
# \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################
AUTOMAKE_OPTIONS = foreign nostdinc
EXTRA_DIST = CMakeLists.txt .checksrc README.md Makefile.example \
$(COMPLICATED_MAY_BUILD) $(COMPLICATED_EXAMPLES)
# Specify our include paths here, and do it relative to $(top_srcdir) and
# $(top_builddir), to ensure that these paths which belong to the library
# being currently built and tested are searched before the library which
# might possibly already be installed in the system.
#
# $(top_srcdir)/include is for libcurl's external include files
AM_CPPFLAGS = -I$(top_srcdir)/include
if USE_CPPFLAG_CURL_STATICLIB
AM_CPPFLAGS += -DCURL_STATICLIB
endif
if DOING_NATIVE_WINDOWS
AM_CPPFLAGS += -DWIN32_LEAN_AND_MEAN
endif
# Avoid libcurl obsolete stuff
AM_CPPFLAGS += -DCURL_NO_OLDIES
# Prevent LIBS from being used for all link targets
LIBS = $(BLANK_AT_MAKETIME)
# Dependencies
LDADD = $(top_builddir)/lib/libcurl.la
# This might hold -Werror
CFLAGS += @CURL_CFLAG_EXTRAS@
# Get check_PROGRAMS, COMPLICATED_MAY_BUILD, COMPLICATED_EXAMPLES variables
include Makefile.inc
all: $(check_PROGRAMS)
CHECKSRC = $(CS_$(V))
CS_0 = @echo " RUN " $@;
CS_1 =
CS_ = $(CS_0)
checksrc:
$(CHECKSRC)(@PERL@ $(top_srcdir)/scripts/checksrc.pl -D$(srcdir) $(srcdir)/*.c)
if NOT_CURL_CI
if DEBUGBUILD
# for debug builds, we scan the sources on all regular make invokes
all-local: checksrc
endif
endif
+50
View File
@@ -0,0 +1,50 @@
#***************************************************************************
# _ _ ____ _
# Project ___| | | | _ \| |
# / __| | | | |_) | |
# | (__| |_| | _ <| |___
# \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################
SRC ?= https.c
# What to call the final executable
TARGET ?= example
# What compiler to use
CC ?= gcc
# Compiler flags, -g for debug
CFLAGS ?= -g
# This should point to a directory that holds libcurl, if it is not in the
# system's standard lib dir
# We also set a -L to include the directory where we have the OpenSSL libraries
LDFLAGS ?= -L/usr/lib -L/usr/local/lib
# We need -lsocket and -lnsl when on Solaris
# We need -lssl and -lcrypto when using libcurl with TLS support
# We need -lpthread for the pthread example
LIBS ?= -lssl -lcrypto
# We need -lcurl for the curl stuff
LIBS := -lcurl $(LIBS)
# Link the target with all objects and libraries
$(TARGET) : $(SRC)
$(CC) $< $(CFLAGS) $(LDFLAGS) $(LIBS) -o $(TARGET)
File diff suppressed because it is too large Load Diff
+167
View File
@@ -0,0 +1,167 @@
#***************************************************************************
# _ _ ____ _
# Project ___| | | | _ \| |
# / __| | | | |_) | |
# | (__| |_| | _ <| |___
# \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################
# Shared between CMakeLists.txt and Makefile.am
# These are all libcurl example programs to be test compiled
check_PROGRAMS = \
10-at-a-time \
address-scope \
altsvc \
anyauthput \
block_ip \
certinfo \
chkspeed \
connect-to \
cookie_interface \
debug \
default-scheme \
externalsocket \
fileupload \
ftp-delete \
ftp-wildcard \
ftpget \
ftpgetinfo \
ftpgetresp \
ftpsget \
ftpupload \
ftpuploadfrommem \
ftpuploadresume \
getinfo \
getinmemory \
getredirect \
getreferrer \
headerapi \
hsts-preload \
http-options \
http-post \
http2-download \
http2-pushinmemory \
http2-serverpush \
http2-upload \
http3 \
http3-present \
httpcustomheader \
httpput \
httpput-postfields \
https \
imap-append \
imap-authzid \
imap-copy \
imap-create \
imap-delete \
imap-examine \
imap-fetch \
imap-list \
imap-lsub \
imap-multi \
imap-noop \
imap-search \
imap-ssl \
imap-store \
imap-tls \
interface \
ipv6 \
keepalive \
localport \
log_failed_transfers \
maxconnects \
multi-app \
multi-debugcallback \
multi-double \
multi-formadd \
multi-legacy \
multi-post \
multi-single \
netrc \
parseurl \
persistent \
pop3-authzid \
pop3-dele \
pop3-list \
pop3-multi \
pop3-noop \
pop3-retr \
pop3-ssl \
pop3-stat \
pop3-tls \
pop3-top \
pop3-uidl \
post-callback \
postinmemory \
postit2 \
postit2-formadd \
progressfunc \
protofeats \
range \
resolve \
rtsp-options \
sendrecv \
sepheaders \
sftpget \
sftpuploadresume \
shared-connection-cache \
simple \
simplepost \
simplessl \
smtp-authzid \
smtp-expn \
smtp-mail \
smtp-mime \
smtp-multi \
smtp-ssl \
smtp-tls \
smtp-vrfy \
sslbackend \
synctime \
unixsocket \
url2file \
urlapi \
websocket \
websocket-cb \
websocket-updown
# These examples require external dependencies that may be available during
# the build.
COMPLICATED_MAY_BUILD = \
cacertinmem.c \
multi-uv.c \
multithread.c \
sessioninfo.c \
threaded-ssl.c \
usercertinmem.c
# These examples require external dependencies that may not be commonly
# available on POSIX systems, so do not bother attempting to compile them here.
COMPLICATED_EXAMPLES = \
crawler.c \
ephiperfifo.c \
evhiperfifo.c \
ghiper.c \
hiperfifo.c \
htmltidy.c \
htmltitle.cpp \
multi-event.c \
smooth-gtk-thread.c \
version-check.pl \
xmlstream.c
+41
View File
@@ -0,0 +1,41 @@
<!--
Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
SPDX-License-Identifier: curl
-->
# libcurl examples
This directory is for libcurl programming examples. They are meant to show
some simple steps on how you can build your own application to take full
advantage of libcurl.
If you end up with other small but still useful example sources, please mail
them for submission in future packages and on the website.
## Building
The `Makefile.example` is an example Makefile that could be used to build
these examples. Just edit the file according to your system and requirements
first.
Most examples should build fine using a command line like this:
`curl-config --cc --cflags --libs` -o example-my example.c
Some compilers do not like having the arguments in this order but instead
want you do reorganize them like:
`curl-config --cc` -o example-my example.c `curl-config --cflags --libs`
**Please** do not use the `curl.se` site as a test target for your libcurl
applications/experiments. Even if some of the examples use that site as a URL
at some places, it does not mean that the URLs work or that we expect you to
actually torture our website with your tests. Thanks.
## Examples
Each example source code file is designed to be and work stand-alone and
rather self-explanatory. The examples may at times lack the level of error
checks you need in a real world, but that is then only for the sake of
readability: to make the code smaller and easier to follow.
+68
View File
@@ -0,0 +1,68 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* HTTP GET to an IPv6 address with specific scope
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
#if !defined(_WIN32) && !defined(MSDOS) && !defined(__AMIGA__)
#include <net/if.h>
#endif
int main(void)
{
#if !defined(_WIN32) && !defined(MSDOS) && !defined(__AMIGA__)
/* Windows/MS-DOS users need to find how to use if_nametoindex() */
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
long my_scope_id;
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
my_scope_id = (long)if_nametoindex("eth0");
curl_easy_setopt(curl, CURLOPT_ADDRESS_SCOPE, my_scope_id);
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
#else
return 0;
#endif
}
+62
View File
@@ -0,0 +1,62 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* HTTP with Alt-Svc support
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
/* cache the alternatives in this file */
curl_easy_setopt(curl, CURLOPT_ALTSVC, "altsvc.txt");
/* restrict which HTTP versions to use alternatives */
curl_easy_setopt(curl, CURLOPT_ALTSVC_CTRL,
CURLALTSVC_H1 | CURLALTSVC_H2 | CURLALTSVC_H3);
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+172
View File
@@ -0,0 +1,172 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* HTTP PUT upload with authentication using "any" method. libcurl picks the
* one the server supports/wants.
* </DESC>
*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <curl/curl.h>
#ifdef _WIN32
#undef stat
#define stat _stat
#undef fstat
#define fstat _fstat
#define fileno _fileno
#endif
#if LIBCURL_VERSION_NUM < 0x070c03
#error "upgrade your libcurl to no less than 7.12.3"
#endif
/*
* This example shows an HTTP PUT operation with authentication using "any"
* type. It PUTs a file given as a command line argument to the URL also given
* on the command line.
*
* Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set seek
* function.
*
* This example also uses its own read callback.
*/
/* seek callback function */
static int my_seek(void *userp, curl_off_t offset, int origin)
{
FILE *fp = (FILE *) userp;
if(fseek(fp, (long) offset, origin) == -1)
/* could not seek */
return CURL_SEEKFUNC_CANTSEEK;
return CURL_SEEKFUNC_OK; /* success! */
}
/* read callback function, fread() look alike */
static size_t read_cb(char *ptr, size_t size, size_t nmemb, void *stream)
{
size_t nread;
nread = fread(ptr, size, nmemb, stream);
if(nread > 0) {
fprintf(stderr, "*** We read %lu bytes from file\n", (unsigned long)nread);
}
return nread;
}
int main(int argc, char **argv)
{
CURL *curl;
CURLcode res;
FILE *fp;
struct stat file_info;
char *file;
char *url;
if(argc < 3)
return 1;
file = argv[1];
url = argv[2];
/* get the file size of the local file */
fp = fopen(file, "rb");
if(!fp)
return 2;
#ifdef UNDER_CE
/* !checksrc! disable BANNEDFUNC 1 */
if(stat(file, &file_info) != 0) {
#else
if(fstat(fileno(fp), &file_info) != 0) {
#endif
fclose(fp);
return 1; /* cannot continue */
}
/* In Windows, this inits the Winsock stuff */
res = curl_global_init(CURL_GLOBAL_ALL);
if(res) {
fclose(fp);
return (int)res;
}
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
/* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb);
/* which file to upload */
curl_easy_setopt(curl, CURLOPT_READDATA, (void *) fp);
/* set the seek function */
curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, my_seek);
/* pass the file descriptor to the seek callback as well */
curl_easy_setopt(curl, CURLOPT_SEEKDATA, (void *) fp);
/* enable "uploading" (which means PUT when doing HTTP) */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* specify target URL, and note that this URL should also include a file
name, not only a directory (as you can do with GTP uploads) */
curl_easy_setopt(curl, CURLOPT_URL, url);
/* and give the size of the upload, this supports large file sizes
on systems that have general support for it */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)file_info.st_size);
/* tell libcurl we can use "any" auth, which lets the lib pick one, but it
also costs one extra round-trip and possibly sending of all the PUT
data twice!!! */
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
/* set user name and password for the authentication */
curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
/* Now run off and do what you have been told! */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(fp); /* close the local file */
curl_global_cleanup();
return (int)res;
}
+357
View File
@@ -0,0 +1,357 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Show how CURLOPT_OPENSOCKETFUNCTION can be used to block IP addresses.
* </DESC>
*/
/* This is an advanced example that defines a whitelist or a blacklist to
* filter IP addresses.
*/
#if defined(__AMIGA__) || defined(UNDER_CE)
#include <stdio.h>
int main(void) { printf("Platform not supported.\n"); return 1; }
#else
#ifdef _WIN32
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#endif
#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0600 /* Requires Windows Vista */
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
struct ip {
/* The user-provided IP address or network (use CIDR) to filter */
char *str;
/* IP address family AF_INET (IPv4) or AF_INET6 (IPv6) */
int family;
/* IP in network byte format */
union netaddr {
struct in_addr ipv4;
#ifdef AF_INET6
struct in6_addr ipv6;
#endif
} netaddr;
/* IP bits to match against.
* This is equal to the CIDR notation or max bits if no CIDR.
* For example if ip->str is 127.0.0.0/8 then ip->maskbits is 8.
*/
int maskbits;
struct ip *next;
};
enum connection_filter_t {
CONNECTION_FILTER_BLACKLIST,
CONNECTION_FILTER_WHITELIST
};
struct connection_filter {
struct ip *list;
enum connection_filter_t type;
int verbose;
#ifdef AF_INET6
/* If the address being filtered is an IPv4-mapped IPv6 address then it is
* checked against IPv4 list entries as well, unless ipv6_v6only is set TRUE.
*/
int ipv6_v6only;
#endif
};
static struct ip *ip_list_append(struct ip *list, const char *data)
{
struct ip *ip, *last;
char *cidr;
ip = (struct ip *)calloc(1, sizeof(*ip));
if(!ip)
return NULL;
if(strchr(data, ':')) {
#ifdef AF_INET6
ip->family = AF_INET6;
#else
free(ip);
return NULL;
#endif
}
else
ip->family = AF_INET;
ip->str = strdup(data);
if(!ip->str) {
free(ip);
return NULL;
}
/* determine the number of bits that this IP will match against */
cidr = strchr(ip->str, '/');
if(cidr) {
ip->maskbits = atoi(cidr + 1);
if(ip->maskbits <= 0 ||
#ifdef AF_INET6
(ip->family == AF_INET6 && ip->maskbits > 128) ||
#endif
(ip->family == AF_INET && ip->maskbits > 32)) {
free(ip->str);
free(ip);
return NULL;
}
/* ignore the CIDR notation when converting ip->str to ip->netaddr */
*cidr = '\0';
}
else if(ip->family == AF_INET)
ip->maskbits = 32;
#ifdef AF_INET6
else if(ip->family == AF_INET6)
ip->maskbits = 128;
#endif
if(inet_pton(ip->family, ip->str, &ip->netaddr) != 1) {
free(ip->str);
free(ip);
return NULL;
}
if(cidr)
*cidr = '/';
if(!list)
return ip;
for(last = list; last->next; last = last->next)
;
last->next = ip;
return list;
}
static void ip_list_free_all(struct ip *list)
{
struct ip *next;
while(list) {
next = list->next;
free(list->str);
free(list);
list = next;
}
}
static void free_connection_filter(struct connection_filter *filter)
{
if(filter) {
ip_list_free_all(filter->list);
free(filter);
}
}
static int ip_match(struct ip *ip, void *netaddr)
{
int bytes, tailbits;
const unsigned char *x, *y;
x = (unsigned char *)&ip->netaddr;
y = (unsigned char *)netaddr;
for(bytes = ip->maskbits / 8; bytes; --bytes) {
if(*x++ != *y++)
return FALSE;
}
tailbits = ip->maskbits % 8;
if(tailbits) {
unsigned char tailmask = (unsigned char)((0xFF << (8 - tailbits)) & 0xFF);
if((*x & tailmask) != (*y & tailmask))
return FALSE;
}
return TRUE;
}
#ifdef AF_INET6
static int is_ipv4_mapped_ipv6_address(int family, void *netaddr)
{
if(family == AF_INET6) {
int i;
unsigned char *x = (unsigned char *)netaddr;
for(i = 0; i < 12; ++i) {
if(x[i])
break;
}
/* support formats ::x.x.x.x (deprecated) and ::ffff:x.x.x.x */
if((i == 12 && (x[i] || x[i + 1] || x[i + 2] || x[i + 3])) ||
(i == 10 && (x[i] == 0xFF && x[i + 1] == 0xFF)))
return TRUE;
}
return FALSE;
}
#endif /* AF_INET6 */
static curl_socket_t opensocket(void *clientp,
curlsocktype purpose,
struct curl_sockaddr *address)
{
/* filter the address */
if(purpose == CURLSOCKTYPE_IPCXN) {
void *cinaddr = NULL;
if(address->family == AF_INET)
cinaddr = &((struct sockaddr_in *)(void *)&address->addr)->sin_addr;
#ifdef AF_INET6
else if(address->family == AF_INET6)
cinaddr = &((struct sockaddr_in6 *)(void *)&address->addr)->sin6_addr;
#endif
if(cinaddr) {
struct ip *ip;
struct connection_filter *filter = (struct connection_filter *)clientp;
#ifdef AF_INET6
int mapped = !filter->ipv6_v6only &&
is_ipv4_mapped_ipv6_address(address->family, cinaddr);
#endif
for(ip = filter->list; ip; ip = ip->next) {
if(ip->family == address->family && ip_match(ip, cinaddr))
break;
#ifdef AF_INET6
if(mapped && ip->family == AF_INET && address->family == AF_INET6 &&
ip_match(ip, (unsigned char *)cinaddr + 12))
break;
#endif
}
if(ip && filter->type == CONNECTION_FILTER_BLACKLIST) {
if(filter->verbose) {
char buf[128] = {0};
inet_ntop(address->family, cinaddr, buf, sizeof(buf));
fprintf(stderr, "* Rejecting IP %s due to blacklist entry %s.\n",
buf, ip->str);
}
return CURL_SOCKET_BAD;
}
else if(!ip && filter->type == CONNECTION_FILTER_WHITELIST) {
if(filter->verbose) {
char buf[128] = {0};
inet_ntop(address->family, cinaddr, buf, sizeof(buf));
fprintf(stderr,
"* Rejecting IP %s due to missing whitelist entry.\n", buf);
}
return CURL_SOCKET_BAD;
}
}
}
return socket(address->family, address->socktype, address->protocol);
}
int main(void)
{
CURL *curl;
CURLcode res;
struct connection_filter *filter;
filter = (struct connection_filter *)calloc(1, sizeof(*filter));
if(!filter)
return 1;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res) {
free(filter);
return (int)res;
}
curl = curl_easy_init();
if(!curl) {
curl_global_cleanup();
free(filter);
return 1;
}
/* Set the target URL */
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost");
/* Define an IP connection filter.
* If an address has CIDR notation then it matches the network.
* For example 74.6.143.25/24 matches 74.6.143.0 - 74.6.143.255.
*/
filter->type = CONNECTION_FILTER_BLACKLIST;
filter->list = ip_list_append(filter->list, "98.137.11.164");
filter->list = ip_list_append(filter->list, "127.0.0.0/8");
#ifdef AF_INET6
filter->list = ip_list_append(filter->list, "::1");
#endif
/* Set the socket function which does the filtering */
curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, filter);
/* Verbose mode */
filter->verbose = TRUE;
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* Perform the request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
/* Clean up */
curl_easy_cleanup(curl);
free_connection_filter(filter);
/* Clean up libcurl */
curl_global_cleanup();
return 0;
}
#endif
+179
View File
@@ -0,0 +1,179 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* CA cert in memory with OpenSSL to get an HTTPS page.
* </DESC>
*/
/* Requires: USE_OPENSSL */
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <curl/curl.h>
#include <stdio.h>
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic ignored "-Woverlength-strings"
#endif
/* Silence warning when calling sk_X509_INFO_pop_free() */
#if defined(__clang__) && __clang_major__ >= 16
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcast-function-type-strict"
#endif
#if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
typedef size_t ossl_valsize_t;
#else
typedef int ossl_valsize_t;
#endif
static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *stream)
{
fwrite(ptr, size, nmemb, (FILE *)stream);
return nmemb * size;
}
static CURLcode sslctx_function(CURL *curl, void *sslctx, void *pointer)
{
/** This example uses two (fake) certificates **/
/* replace the XXX with the actual CA certificates */
static const char mypem[] =
"-----BEGIN CERTIFICATE-----\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
"-----END CERTIFICATE-----\n"
"-----BEGIN CERTIFICATE-----\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
"-----END CERTIFICATE-----\n";
BIO *cbio = BIO_new_mem_buf(mypem, sizeof(mypem));
X509_STORE *cts = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
ossl_valsize_t i;
STACK_OF(X509_INFO) *inf;
(void)curl;
(void)pointer;
if(!cts || !cbio) {
return CURLE_ABORTED_BY_CALLBACK;
}
inf = PEM_X509_INFO_read_bio(cbio, NULL, NULL, NULL);
if(!inf) {
BIO_free(cbio);
return CURLE_ABORTED_BY_CALLBACK;
}
for(i = 0; i < sk_X509_INFO_num(inf); i++) {
X509_INFO *itmp = sk_X509_INFO_value(inf, i);
if(itmp->x509) {
X509_STORE_add_cert(cts, itmp->x509);
}
if(itmp->crl) {
X509_STORE_add_crl(cts, itmp->crl);
}
}
sk_X509_INFO_pop_free(inf, X509_INFO_free);
BIO_free(cbio);
return CURLE_OK;
}
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_cb);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, stderr);
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
/* Turn off the default CA locations, otherwise libcurl loads CA
* certificates from the locations that were detected/specified at
* build-time
*/
curl_easy_setopt(curl, CURLOPT_CAINFO, NULL);
curl_easy_setopt(curl, CURLOPT_CAPATH, NULL);
/* first try: retrieve page without ca certificates -> should fail
* unless libcurl was built --with-ca-fallback enabled at build-time
*/
res = curl_easy_perform(curl);
if(res == CURLE_OK)
printf("*** transfer succeeded ***\n");
else
printf("*** transfer failed ***\n");
/* use a fresh connection (optional) this option seriously impacts
* performance of multiple transfers but it is necessary order to
* demonstrate this example. recall that the ssl ctx callback is only
* called _before_ an SSL connection is established, therefore it does not
* affect existing verified SSL connections already in the connection
* cache associated with this handle. normally you would set the ssl ctx
* function before making any transfers, and not use this option.
*/
curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1L);
/* second try: retrieve page using cacerts' certificate -> succeeds to
* load the certificate by installing a function doing the necessary
* "modifications" to the SSL CONTEXT just before link init
*/
curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, sslctx_function);
res = curl_easy_perform(curl);
if(res == CURLE_OK)
printf("*** transfer succeeded ***\n");
else
printf("*** transfer failed ***\n");
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+89
View File
@@ -0,0 +1,89 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Extract lots of TLS certificate info.
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *stream)
{
(void)stream;
(void)ptr;
return size * nmemb;
}
int main(void)
{
CURL *curl;
CURLcode res;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
curl_easy_setopt(curl, CURLOPT_CERTINFO, 1L);
res = curl_easy_perform(curl);
if(!res) {
struct curl_certinfo *certinfo;
res = curl_easy_getinfo(curl, CURLINFO_CERTINFO, &certinfo);
if(!res && certinfo) {
int i;
printf("%d certs!\n", certinfo->num_of_certs);
for(i = 0; i < certinfo->num_of_certs; i++) {
struct curl_slist *slist;
for(slist = certinfo->certinfo[i]; slist; slist = slist->next)
printf("%s\n", slist->data);
}
}
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+235
View File
@@ -0,0 +1,235 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Show transfer timing info after download completes.
* </DESC>
*/
/* Example source code to show how the callback function can be used to
* download data into a chunk of memory instead of storing it in a file.
* After successful download we use curl_easy_getinfo() calls to get the
* amount of downloaded bytes, the time used for the whole download, and
* the average download speed.
* On Linux you can create the download test files with:
* dd if=/dev/urandom of=file_1M.bin bs=1M count=1
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <curl/curl.h>
#define URL_BASE "http://speedtest.your.domain/"
#define URL_1M URL_BASE "file_1M.bin"
#define URL_2M URL_BASE "file_2M.bin"
#define URL_5M URL_BASE "file_5M.bin"
#define URL_10M URL_BASE "file_10M.bin"
#define URL_20M URL_BASE "file_20M.bin"
#define URL_50M URL_BASE "file_50M.bin"
#define URL_100M URL_BASE "file_100M.bin"
#define CHKSPEED_VERSION "1.0"
static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
{
/* we are not interested in the downloaded bytes itself,
so we only return the size we would have saved ... */
(void)ptr;
(void)data;
return (size_t)(size * nmemb);
}
int main(int argc, char *argv[])
{
CURL *curl;
CURLcode res;
int prtall = 0, prtsep = 0, prttime = 0;
const char *url = URL_1M;
char *appname = argv[0];
if(argc > 1) {
/* parse input parameters */
for(argc--, argv++; *argv; argc--, argv++) {
if(argv[0][0] == '-') {
switch(argv[0][1]) {
case 'h':
case 'H':
fprintf(stderr,
"\rUsage: %s [-m=1|2|5|10|20|50|100] [-t] [-x] [url]\n",
appname);
return 1;
case 'v':
case 'V':
fprintf(stderr, "\r%s %s - %s\n",
appname, CHKSPEED_VERSION, curl_version());
return 1;
case 'a':
case 'A':
prtall = 1;
break;
case 'x':
case 'X':
prtsep = 1;
break;
case 't':
case 'T':
prttime = 1;
break;
case 'm':
case 'M':
if(argv[0][2] == '=') {
int m = atoi((*argv) + 3);
switch(m) {
case 1:
url = URL_1M;
break;
case 2:
url = URL_2M;
break;
case 5:
url = URL_5M;
break;
case 10:
url = URL_10M;
break;
case 20:
url = URL_20M;
break;
case 50:
url = URL_50M;
break;
case 100:
url = URL_100M;
break;
default:
fprintf(stderr, "\r%s: invalid parameter %s\n",
appname, *argv + 3);
return 1;
}
break;
}
fprintf(stderr, "\r%s: invalid or unknown option %s\n",
appname, *argv);
return 1;
default:
fprintf(stderr, "\r%s: invalid or unknown option %s\n",
appname, *argv);
return 1;
}
}
else {
url = *argv;
}
}
}
/* print separator line */
if(prtsep) {
printf("-------------------------------------------------\n");
}
/* print localtime */
if(prttime) {
time_t t = time(NULL);
printf("Localtime: %s", ctime(&t));
}
/* init libcurl */
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
/* init the curl session */
curl = curl_easy_init();
if(curl) {
/* specify URL to get */
curl_easy_setopt(curl, CURLOPT_URL, url);
/* send all data to this function */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
/* some servers do not like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(curl, CURLOPT_USERAGENT,
"libcurl-speedchecker/" CHKSPEED_VERSION);
/* get it! */
res = curl_easy_perform(curl);
if(CURLE_OK == res) {
curl_off_t val;
/* check for bytes downloaded */
res = curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD_T, &val);
if((CURLE_OK == res) && (val > 0))
printf("Data downloaded: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", val);
/* check for total download time */
res = curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &val);
if((CURLE_OK == res) && (val > 0))
printf("Total download time: %" CURL_FORMAT_CURL_OFF_T
".%06" CURL_FORMAT_CURL_OFF_T " sec.\n",
val / 1000000,
val % 1000000);
/* check for average download speed */
res = curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD_T, &val);
if((CURLE_OK == res) && (val > 0))
printf("Average download speed: "
"%" CURL_FORMAT_CURL_OFF_T " kbyte/sec.\n",
val / 1024);
if(prtall) {
/* check for name resolution time */
res = curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME_T, &val);
if((CURLE_OK == res) && (val > 0))
printf("Name lookup time: %" CURL_FORMAT_CURL_OFF_T
".%06" CURL_FORMAT_CURL_OFF_T " sec.\n",
val / 1000000,
val % 1000000);
/* check for connect time */
res = curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME_T, &val);
if((CURLE_OK == res) && (val > 0))
printf("Connect time: %" CURL_FORMAT_CURL_OFF_T
".%06" CURL_FORMAT_CURL_OFF_T " sec.\n",
val / 1000000,
val % 1000000);
}
}
else {
fprintf(stderr, "Error while fetching '%s' : %s\n",
url, curl_easy_strerror(res));
}
/* cleanup curl stuff */
curl_easy_cleanup(curl);
}
/* we are done with libcurl, so clean it up */
curl_global_cleanup();
return 0;
}
+75
View File
@@ -0,0 +1,75 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Use CURLOPT_CONNECT_TO to connect to "wrong" hostname
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
struct curl_slist *host;
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
/*
Each single string should be written using the format
HOST:PORT:CONNECT-TO-HOST:CONNECT-TO-PORT where HOST is the host of the
request, PORT is the port of the request, CONNECT-TO-HOST is the host name
to connect to, and CONNECT-TO-PORT is the port to connect to.
*/
/* instead of curl.se:443, it resolves and uses example.com:443 but in other
aspects work as if it still is curl.se */
host = curl_slist_append(NULL, "curl.se:443:example.com:443");
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CONNECT_TO, host);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_URL, "https://curl.se/");
/* since this connects to the wrong host, checking the host name in the
server certificate fails, so unless we disable the check libcurl
returns CURLE_PEER_FAILED_VERIFICATION */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
/* Letting the wrong host name in the certificate be okay, the transfer
goes through but (most likely) causes a 404 or similar because it sends
an unknown name in the Host: header field */
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_slist_free_all(host);
curl_global_cleanup();
return (int)res;
}
+145
View File
@@ -0,0 +1,145 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Import and export cookies with COOKIELIST.
* </DESC>
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <curl/curl.h>
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
static int print_cookies(CURL *curl)
{
CURLcode res;
struct curl_slist *cookies;
struct curl_slist *nc;
int i;
printf("Cookies, curl knows:\n");
res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
if(res != CURLE_OK) {
fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n",
curl_easy_strerror(res));
return 1;
}
nc = cookies;
i = 1;
while(nc) {
printf("[%d]: %s\n", i, nc->data);
nc = nc->next;
i++;
}
if(i == 1) {
printf("(none)\n");
}
curl_slist_free_all(cookies);
return 0;
}
int
main(void)
{
CURL *curl;
CURLcode res;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
char nline[512];
curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* start cookie engine */
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
return 1;
}
print_cookies(curl);
printf("Erasing curl's knowledge of cookies!\n");
curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
print_cookies(curl);
printf("-----------------------------------------------\n"
"Setting a cookie \"PREF\" via cookie interface:\n");
/* Netscape format cookie */
snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%.0f\t%s\t%s",
".example.com", "TRUE", "/", "FALSE",
difftime(time(NULL) + 31337, (time_t)0),
"PREF", "hello example, I like you!");
res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
if(res != CURLE_OK) {
fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
curl_easy_strerror(res));
return 1;
}
/* HTTP-header style cookie. If you use the Set-Cookie format and do not
specify a domain then the cookie is sent for any domain and is not
modified, likely not what you intended. For more information refer to
the CURLOPT_COOKIELIST documentation.
*/
snprintf(nline, sizeof(nline),
"Set-Cookie: OLD_PREF=3d141414bf4209321; "
"expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.example.com");
res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
if(res != CURLE_OK) {
fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
curl_easy_strerror(res));
return 1;
}
print_cookies(curl);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
return 1;
}
curl_easy_cleanup(curl);
}
else {
fprintf(stderr, "Curl init failed!\n");
return 1;
}
curl_global_cleanup();
return 0;
}
+260
View File
@@ -0,0 +1,260 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Jeroen Ooms <jeroenooms@gmail.com>
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
* To compile:
* gcc crawler.c $(pkg-config --cflags --libs libxml-2.0 libcurl)
*
*/
/* <DESC>
* Web crawler based on curl and libxml2 to stress-test curl with
* hundreds of concurrent connections to various servers.
* </DESC>
*/
#include <libxml/HTMLparser.h>
#include <libxml/xpath.h>
#include <libxml/uri.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <signal.h>
/* Parameters */
static int max_con = 200;
static int max_total = 20000;
static int max_requests = 500;
static size_t max_link_per_page = 5;
static int follow_relative_links = 0;
static const char *start_page = "https://www.reuters.com";
static int pending_interrupt = 0;
static void sighandler(int dummy)
{
(void)dummy;
pending_interrupt = 1;
}
/* resizable buffer */
struct memory {
char *buf;
size_t size;
};
static size_t write_cb(void *contents, size_t sz, size_t nmemb, void *ctx)
{
size_t realsize = sz * nmemb;
struct memory *mem = (struct memory*) ctx;
char *ptr = realloc(mem->buf, mem->size + realsize);
if(!ptr) {
/* out of memory */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
mem->buf = ptr;
memcpy(&(mem->buf[mem->size]), contents, realsize);
mem->size += realsize;
return realsize;
}
static CURL *make_handle(const char *url)
{
CURL *curl = curl_easy_init();
struct memory *mem;
/* Important: use HTTP2 over HTTPS */
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
curl_easy_setopt(curl, CURLOPT_URL, url);
/* buffer body */
mem = malloc(sizeof(*mem));
mem->size = 0;
mem->buf = malloc(1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, mem);
curl_easy_setopt(curl, CURLOPT_PRIVATE, mem);
/* For completeness */
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* only allow redirects to HTTP and HTTPS URLs */
curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS_STR, "http,https");
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1L);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10L);
/* each transfer needs to be done within 20 seconds! */
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 20000L);
/* connect fast or fail */
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 2000L);
/* skip files larger than a gigabyte */
curl_easy_setopt(curl, CURLOPT_MAXFILESIZE_LARGE,
(curl_off_t)1024*1024*1024);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "mini crawler");
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_easy_setopt(curl, CURLOPT_UNRESTRICTED_AUTH, 1L);
curl_easy_setopt(curl, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
curl_easy_setopt(curl, CURLOPT_EXPECT_100_TIMEOUT_MS, 0L);
return curl;
}
/* HREF finder implemented in libxml2 but could be any HTML parser */
static size_t follow_links(CURLM *multi, struct memory *mem,
const char *url)
{
int opts = HTML_PARSE_NOBLANKS | HTML_PARSE_NOERROR | \
HTML_PARSE_NOWARNING | HTML_PARSE_NONET;
htmlDocPtr doc = htmlReadMemory(mem->buf, (int)mem->size, url, NULL, opts);
size_t count;
int i;
xmlChar *xpath;
xmlNodeSetPtr nodeset;
xmlXPathContextPtr context;
xmlXPathObjectPtr result;
if(!doc)
return 0;
xpath = (xmlChar*) "//a/@href";
context = xmlXPathNewContext(doc);
result = xmlXPathEvalExpression(xpath, context);
xmlXPathFreeContext(context);
if(!result)
return 0;
nodeset = result->nodesetval;
if(xmlXPathNodeSetIsEmpty(nodeset)) {
xmlXPathFreeObject(result);
return 0;
}
count = 0;
for(i = 0; i < nodeset->nodeNr; i++) {
double r = rand();
int x = (int)(r * nodeset->nodeNr / RAND_MAX);
const xmlNode *node = nodeset->nodeTab[x]->xmlChildrenNode;
xmlChar *href = xmlNodeListGetString(doc, node, 1);
char *link;
if(follow_relative_links) {
xmlChar *orig = href;
href = xmlBuildURI(href, (xmlChar *) url);
xmlFree(orig);
}
link = (char *) href;
if(!link || strlen(link) < 20)
continue;
if(!strncmp(link, "http://", 7) || !strncmp(link, "https://", 8)) {
curl_multi_add_handle(multi, make_handle(link));
if(count++ == max_link_per_page)
break;
}
xmlFree(link);
}
xmlXPathFreeObject(result);
return count;
}
static int is_html(char *ctype)
{
return ctype != NULL && strlen(ctype) > 10 && strstr(ctype, "text/html");
}
int main(void)
{
CURLM *multi;
int msgs_left;
int pending;
int complete;
int still_running;
CURLcode res;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
signal(SIGINT, sighandler);
LIBXML_TEST_VERSION
multi = curl_multi_init();
if(multi) {
curl_multi_setopt(multi, CURLMOPT_MAX_TOTAL_CONNECTIONS, max_con);
curl_multi_setopt(multi, CURLMOPT_MAX_HOST_CONNECTIONS, 6L);
/* enables http/2 if available */
#ifdef CURLPIPE_MULTIPLEX
curl_multi_setopt(multi, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
#endif
/* sets html start page */
curl_multi_add_handle(multi, make_handle(start_page));
pending = 0;
complete = 0;
still_running = 1;
while(still_running && !pending_interrupt) {
int numfds;
CURLMsg *m;
curl_multi_wait(multi, NULL, 0, 1000, &numfds);
curl_multi_perform(multi, &still_running);
/* See how the transfers went */
m = NULL;
while((m = curl_multi_info_read(multi, &msgs_left))) {
if(m->msg == CURLMSG_DONE) {
CURL *curl = m->easy_handle;
char *url;
struct memory *mem;
curl_easy_getinfo(curl, CURLINFO_PRIVATE, &mem);
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
if(m->data.result == CURLE_OK) {
long res_status;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &res_status);
if(res_status == 200) {
char *ctype;
curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ctype);
printf("[%d] HTTP 200 (%s): %s\n", complete, ctype, url);
if(is_html(ctype) && mem->size > 100) {
if(pending < max_requests &&
(complete + pending) < max_total) {
pending += follow_links(multi_curl, mem, url);
still_running = 1;
}
}
}
else {
printf("[%d] HTTP %d: %s\n", complete, (int) res_status, url);
}
}
else {
printf("[%d] Connection failure: %s\n", complete, url);
}
curl_multi_remove_handle(multi, curl);
curl_easy_cleanup(curl);
free(mem->buf);
free(mem);
complete++;
pending--;
}
}
}
curl_multi_cleanup(multi);
}
curl_global_cleanup();
return 0;
}
+156
View File
@@ -0,0 +1,156 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Show how CURLOPT_DEBUGFUNCTION can be used.
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
struct data {
char trace_ascii; /* 1 or 0 */
};
static void dump(const char *text, FILE *stream, unsigned char *ptr,
size_t size, char nohex)
{
size_t i;
size_t c;
unsigned int width = 0x10;
if(nohex)
/* without the hex output, we can fit more on screen */
width = 0x40;
fprintf(stream, "%s, %10.10lu bytes (0x%8.8lx)\n",
text, (unsigned long)size, (unsigned long)size);
for(i = 0; i < size; i += width) {
fprintf(stream, "%4.4lx: ", (unsigned long)i);
if(!nohex) {
/* hex not disabled, show it */
for(c = 0; c < width; c++)
if(i + c < size)
fprintf(stream, "%02x ", ptr[i + c]);
else
fputs(" ", stream);
}
for(c = 0; (c < width) && (i + c < size); c++) {
/* check for 0D0A; if found, skip past and start a new line of output */
if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
ptr[i + c + 1] == 0x0A) {
i += (c + 2 - width);
break;
}
fprintf(stream, "%c",
(ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
/* check again for 0D0A, to avoid an extra \n if it's at width */
if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
ptr[i + c + 2] == 0x0A) {
i += (c + 3 - width);
break;
}
}
fputc('\n', stream); /* newline */
}
fflush(stream);
}
static int my_trace(CURL *curl, curl_infotype type,
char *data, size_t size, void *userp)
{
struct data *config = (struct data *)userp;
const char *text;
(void)curl;
switch(type) {
case CURLINFO_TEXT:
fprintf(stderr, "== Info: %s", data);
return 0;
case CURLINFO_HEADER_OUT:
text = "=> Send header";
break;
case CURLINFO_DATA_OUT:
text = "=> Send data";
break;
case CURLINFO_SSL_DATA_OUT:
text = "=> Send SSL data";
break;
case CURLINFO_HEADER_IN:
text = "<= Recv header";
break;
case CURLINFO_DATA_IN:
text = "<= Recv data";
break;
case CURLINFO_SSL_DATA_IN:
text = "<= Recv SSL data";
break;
default: /* in case a new one is introduced to shock us */
return 0;
}
dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
return 0;
}
int main(void)
{
CURL *curl;
CURLcode res;
struct data config;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
config.trace_ascii = 1; /* enable ASCII tracing */
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);
/* the DEBUGFUNCTION has no effect until we enable VERBOSE */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+62
View File
@@ -0,0 +1,62 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Change default scheme when none is provided in the URL
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "example.com");
/* Change the default protocol (scheme) for schemeless URLs from plain
"http" to use a secure one instead. */
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+543
View File
@@ -0,0 +1,543 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* multi socket API usage with epoll and timerfd
* </DESC>
*/
/* Example application source code using the multi socket interface to
* download many files at once.
*
* This example features the same basic functionality as hiperfifo.c does,
* but this uses epoll and timerfd instead of libevent.
*
* Written by Jeff Pohlmeyer, converted to use epoll by Josh Bialkowski
Requires a Linux system with epoll
When running, the program creates the named pipe "hiper.fifo"
Whenever there is input into the fifo, the program reads the input as a list
of URL's and creates some new easy handles to fetch each URL via the
curl_multi "hiper" API.
Thus, you can try a single URL:
% echo http://www.yahoo.com > hiper.fifo
Or a whole bunch of them:
% cat my-url-list > hiper.fifo
The fifo buffer is handled almost instantly, so you can even add more URL's
while the previous requests are still being downloaded.
Note:
For the sake of simplicity, URL length is limited to 1023 char's !
This is purely a demo app, all retrieved data is simply discarded by the write
callback.
*/
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/timerfd.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <curl/curl.h>
#define MSG_OUT stdout /* Send info to stdout, change to stderr if you want */
/* Global information, common to all connections */
struct GlobalInfo {
int epfd; /* epoll filedescriptor */
int tfd; /* timer filedescriptor */
int fifofd; /* fifo filedescriptor */
CURLM *multi;
int still_running;
FILE *input;
};
/* Information associated with a specific easy handle */
struct ConnInfo {
CURL *curl;
char *url;
struct GlobalInfo *global;
char error[CURL_ERROR_SIZE];
};
/* Information associated with a specific socket */
struct SockInfo {
curl_socket_t sockfd;
CURL *curl;
int action;
long timeout;
struct GlobalInfo *global;
};
#define mycase(code) \
case code: s = __STRING(code)
/* Die if we get a bad CURLMcode somewhere */
static void mcode_or_die(const char *where, CURLMcode code)
{
if(CURLM_OK != code) {
const char *s;
switch(code) {
mycase(CURLM_BAD_HANDLE); break;
mycase(CURLM_BAD_EASY_HANDLE); break;
mycase(CURLM_OUT_OF_MEMORY); break;
mycase(CURLM_INTERNAL_ERROR); break;
mycase(CURLM_UNKNOWN_OPTION); break;
mycase(CURLM_LAST); break;
default: s = "CURLM_unknown"; break;
mycase(CURLM_BAD_SOCKET);
fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
/* ignore this error */
return;
}
fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
exit(code);
}
}
static void timer_cb(struct GlobalInfo *g, int revents);
/* Update the timer after curl_multi library does its thing. Curl informs the
* application through this callback what it wants the new timeout to be,
* after it does some work. */
static int multi_timer_cb(CURLM *multi, long timeout_ms, struct GlobalInfo *g)
{
struct itimerspec its;
fprintf(MSG_OUT, "multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms);
if(timeout_ms > 0) {
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
its.it_value.tv_sec = timeout_ms / 1000;
its.it_value.tv_nsec = (timeout_ms % 1000) * 1000 * 1000;
}
else if(timeout_ms == 0) {
/* libcurl wants us to timeout now, however setting both fields of
* new_value.it_value to zero disarms the timer. The closest we can
* do is to schedule the timer to fire in 1 ns. */
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = 1;
}
else {
memset(&its, 0, sizeof(its));
}
timerfd_settime(g->tfd, /* flags= */0, &its, NULL);
return 0;
}
/* Check for completed transfers, and remove their easy handles */
static void check_multi_info(struct GlobalInfo *g)
{
char *eff_url;
CURLMsg *msg;
int msgs_left;
struct ConnInfo *conn;
fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running);
while((msg = curl_multi_info_read(g->multi, &msgs_left))) {
if(msg->msg == CURLMSG_DONE) {
CURL *curl = msg->easy_handle;
CURLcode res = msg->data.result;
curl_easy_getinfo(curl, CURLINFO_PRIVATE, &conn);
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &eff_url);
fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error);
curl_multi_remove_handle(g->multi, curl);
free(conn->url);
curl_easy_cleanup(curl);
free(conn);
}
}
}
/* Called by libevent when we get action on a multi socket filedescriptor */
static void event_cb(struct GlobalInfo *g, int fd, int revents)
{
CURLMcode rc;
struct itimerspec its;
int action = ((revents & EPOLLIN) ? CURL_CSELECT_IN : 0) |
((revents & EPOLLOUT) ? CURL_CSELECT_OUT : 0);
rc = curl_multi_socket_action(g->multi, fd, action, &g->still_running);
mcode_or_die("event_cb: curl_multi_socket_action", rc);
check_multi_info(g);
if(g->still_running <= 0) {
fprintf(MSG_OUT, "last transfer done, kill timeout\n");
memset(&its, 0, sizeof(its));
timerfd_settime(g->tfd, 0, &its, NULL);
}
}
/* Called by main loop when our timeout expires */
static void timer_cb(struct GlobalInfo *g, int revents)
{
CURLMcode rc;
uint64_t count = 0;
ssize_t err = 0;
err = read(g->tfd, &count, sizeof(uint64_t));
if(err == -1) {
/* Note that we may call the timer callback even if the timerfd is not
* readable. It's possible that there are multiple events stored in the
* epoll buffer (i.e. the timer may have fired multiple times). The event
* count is cleared after the first call so future events in the epoll
* buffer fails to read from the timer. */
if(errno == EAGAIN) {
fprintf(MSG_OUT, "EAGAIN on tfd %d\n", g->tfd);
return;
}
}
if(err != sizeof(uint64_t)) {
fprintf(stderr, "read(tfd) == %ld", err);
perror("read(tfd)");
}
rc = curl_multi_socket_action(g->multi,
CURL_SOCKET_TIMEOUT, 0, &g->still_running);
mcode_or_die("timer_cb: curl_multi_socket_action", rc);
check_multi_info(g);
}
/* Clean up the SockInfo structure */
static void remsock(struct SockInfo *f, struct GlobalInfo *g)
{
if(f) {
if(f->sockfd) {
if(epoll_ctl(g->epfd, EPOLL_CTL_DEL, f->sockfd, NULL))
fprintf(stderr, "EPOLL_CTL_DEL failed for fd: %d : %s\n",
f->sockfd, strerror(errno));
}
free(f);
}
}
/* Assign information to a SockInfo structure */
static void setsock(struct SockInfo *f, curl_socket_t s, CURL *e, int act,
struct GlobalInfo *g)
{
struct epoll_event ev;
int kind = ((act & CURL_POLL_IN) ? EPOLLIN : 0) |
((act & CURL_POLL_OUT) ? EPOLLOUT : 0);
if(f->sockfd) {
if(epoll_ctl(g->epfd, EPOLL_CTL_DEL, f->sockfd, NULL))
fprintf(stderr, "EPOLL_CTL_DEL failed for fd: %d : %s\n",
f->sockfd, strerror(errno));
}
f->sockfd = s;
f->action = act;
f->curl = e;
ev.events = kind;
ev.data.fd = s;
if(epoll_ctl(g->epfd, EPOLL_CTL_ADD, s, &ev))
fprintf(stderr, "EPOLL_CTL_ADD failed for fd: %d : %s\n",
s, strerror(errno));
}
/* Initialize a new SockInfo structure */
static void addsock(curl_socket_t s, CURL *curl, int action,
struct GlobalInfo *g)
{
struct SockInfo *fdp = (struct SockInfo*)calloc(1, sizeof(struct SockInfo));
fdp->global = g;
setsock(fdp, s, curl, action, g);
curl_multi_assign(g->multi, s, fdp);
}
/* CURLMOPT_SOCKETFUNCTION */
static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
{
struct GlobalInfo *g = (struct GlobalInfo*) cbp;
struct SockInfo *fdp = (struct SockInfo*) sockp;
const char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE" };
fprintf(MSG_OUT,
"socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
if(what == CURL_POLL_REMOVE) {
fprintf(MSG_OUT, "\n");
remsock(fdp, g);
}
else {
if(!fdp) {
fprintf(MSG_OUT, "Adding data: %s\n", whatstr[what]);
addsock(s, e, what, g);
}
else {
fprintf(MSG_OUT,
"Changing action from %s to %s\n",
whatstr[fdp->action], whatstr[what]);
setsock(fdp, s, e, what, g);
}
}
return 0;
}
/* CURLOPT_WRITEFUNCTION */
static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
{
(void)ptr;
(void)data;
return size * nmemb;
}
/* CURLOPT_PROGRESSFUNCTION */
static int prog_cb(void *p, double dltotal, double dlnow, double ult,
double uln)
{
struct ConnInfo *conn = (struct ConnInfo *)p;
(void)ult;
(void)uln;
fprintf(MSG_OUT, "Progress: %s (%g/%g)\n", conn->url, dlnow, dltotal);
return 0;
}
/* Create a new easy handle, and add it to the global curl_multi */
static void new_conn(const char *url, struct GlobalInfo *g)
{
struct ConnInfo *conn;
CURLMcode rc;
conn = (struct ConnInfo*)calloc(1, sizeof(*conn));
conn->error[0] = '\0';
conn->curl = curl_easy_init();
if(!conn->curl) {
fprintf(MSG_OUT, "curl_easy_init() failed, exiting!\n");
exit(2);
}
conn->global = g;
conn->url = strdup(url);
curl_easy_setopt(conn->curl, CURLOPT_URL, conn->url);
curl_easy_setopt(conn->curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(conn->curl, CURLOPT_WRITEDATA, conn);
curl_easy_setopt(conn->curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(conn->curl, CURLOPT_ERRORBUFFER, conn->error);
curl_easy_setopt(conn->curl, CURLOPT_PRIVATE, conn);
curl_easy_setopt(conn->curl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(conn->curl, CURLOPT_PROGRESSFUNCTION, prog_cb);
curl_easy_setopt(conn->curl, CURLOPT_PROGRESSDATA, conn);
curl_easy_setopt(conn->curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(conn->curl, CURLOPT_LOW_SPEED_TIME, 3L);
curl_easy_setopt(conn->curl, CURLOPT_LOW_SPEED_LIMIT, 10L);
fprintf(MSG_OUT,
"Adding easy %p to multi %p (%s)\n", conn->curl, g->multi, url);
rc = curl_multi_add_handle(g->multi, conn->curl);
mcode_or_die("new_conn: curl_multi_add_handle", rc);
/* note that the add_handle() sets a timeout to trigger soon so that the
* necessary socket_action() call gets called by this app */
}
/* This gets called whenever data is received from the fifo */
static void fifo_cb(struct GlobalInfo *g, int revents)
{
char s[1024];
long int rv = 0;
int n = 0;
do {
s[0]='\0';
rv = fscanf(g->input, "%1023s%n", s, &n);
s[n]='\0';
if(n && s[0]) {
new_conn(s, g); /* if we read a URL, go get it! */
}
else
break;
} while(rv != EOF);
}
/* Create a named pipe and tell libevent to monitor it */
static const char *fifo = "hiper.fifo";
static int init_fifo(struct GlobalInfo *g)
{
struct stat st;
curl_socket_t sockfd;
struct epoll_event epev;
fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
if(lstat(fifo, &st) == 0) {
if((st.st_mode & S_IFMT) == S_IFREG) {
errno = EEXIST;
perror("lstat");
return 1;
}
}
unlink(fifo);
if(mkfifo(fifo, 0600) == -1) {
perror("mkfifo");
return 1;
}
sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0);
if(sockfd == -1) {
perror("open");
return 1;
}
g->fifofd = sockfd;
g->input = fdopen(sockfd, "r");
epev.events = EPOLLIN;
epev.data.fd = sockfd;
epoll_ctl(g->epfd, EPOLL_CTL_ADD, sockfd, &epev);
fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo);
return 0;
}
static void clean_fifo(struct GlobalInfo *g)
{
epoll_ctl(g->epfd, EPOLL_CTL_DEL, g->fifofd, NULL);
fclose(g->input);
unlink(fifo);
}
int g_should_exit_ = 0;
void sigint_handler(int signo)
{
g_should_exit_ = 1;
}
int main(int argc, char **argv)
{
CURLcode res;
struct GlobalInfo g;
struct itimerspec its;
struct epoll_event ev;
struct epoll_event events[10];
(void)argc;
(void)argv;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
g_should_exit_ = 0;
signal(SIGINT, sigint_handler);
memset(&g, 0, sizeof(g));
g.epfd = epoll_create1(EPOLL_CLOEXEC);
if(g.epfd == -1) {
perror("epoll_create1 failed");
curl_global_cleanup();
return 1;
}
g.tfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
if(g.tfd == -1) {
perror("timerfd_create failed");
curl_global_cleanup();
return 1;
}
memset(&its, 0, sizeof(its));
its.it_interval.tv_sec = 0;
its.it_value.tv_sec = 1;
timerfd_settime(g.tfd, 0, &its, NULL);
ev.events = EPOLLIN;
ev.data.fd = g.tfd;
epoll_ctl(g.epfd, EPOLL_CTL_ADD, g.tfd, &ev);
if(init_fifo(&g)) {
curl_global_cleanup();
return 1;
}
g.multi = curl_multi_init();
/* setup the generic multi interface options we want */
curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g);
curl_multi_setopt(g.multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
curl_multi_setopt(g.multi, CURLMOPT_TIMERDATA, &g);
/* we do not call any curl_multi_socket*() function yet as we have no handles
added! */
fprintf(MSG_OUT, "Entering wait loop\n");
fflush(MSG_OUT);
while(!g_should_exit_) {
int idx;
int err = epoll_wait(g.epfd, events,
sizeof(events)/sizeof(struct epoll_event), 10000);
if(err == -1) {
/* !checksrc! disable ERRNOVAR 1 */
if(errno == EINTR) {
fprintf(MSG_OUT, "note: wait interrupted\n");
continue;
}
else {
perror("epoll_wait");
curl_global_cleanup();
return 1;
}
}
for(idx = 0; idx < err; ++idx) {
if(events[idx].data.fd == g.fifofd) {
fifo_cb(&g, events[idx].events);
}
else if(events[idx].data.fd == g.tfd) {
timer_cb(&g, events[idx].events);
}
else {
event_cb(&g, events[idx].data.fd, events[idx].events);
}
}
}
fprintf(MSG_OUT, "Exiting normally.\n");
fflush(MSG_OUT);
curl_multi_cleanup(g.multi);
clean_fifo(&g);
curl_global_cleanup();
return 0;
}
+447
View File
@@ -0,0 +1,447 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* multi socket interface together with libev
* </DESC>
*/
/* Example application source code using the multi socket interface to
* download many files at once.
*
* This example features the same basic functionality as hiperfifo.c does,
* but this uses libev instead of libevent.
*
* Written by Jeff Pohlmeyer, converted to use libev by Markus Koetter
Requires libev and a (POSIX?) system that has mkfifo().
This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c"
sample programs.
When running, the program creates the named pipe "hiper.fifo"
Whenever there is input into the fifo, the program reads the input as a list
of URL's and creates some new easy handles to fetch each URL via the
curl_multi "hiper" API.
Thus, you can try a single URL:
% echo http://www.yahoo.com > hiper.fifo
Or a whole bunch of them:
% cat my-url-list > hiper.fifo
The fifo buffer is handled almost instantly, so you can even add more URL's
while the previous requests are still being downloaded.
Note:
For the sake of simplicity, URL length is limited to 1023 char's !
This is purely a demo app, all retrieved data is simply discarded by the write
callback.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <sys/poll.h>
#include <curl/curl.h>
#include <ev.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#define MSG_OUT stdout /* Send info to stdout, change to stderr if you want */
/* Global information, common to all connections */
struct GlobalInfo {
struct ev_loop *loop;
struct ev_io fifo_event;
struct ev_timer timer_event;
CURLM *multi;
int still_running;
FILE *input;
};
/* Information associated with a specific easy handle */
struct ConnInfo {
CURL *curl;
char *url;
struct GlobalInfo *global;
char error[CURL_ERROR_SIZE];
};
/* Information associated with a specific socket */
struct SockInfo {
curl_socket_t sockfd;
CURL *curl;
int action;
long timeout;
struct ev_io ev;
int evset;
struct GlobalInfo *global;
};
static void timer_cb(EV_P_ struct ev_timer *w, int revents);
/* Update the event timer after curl_multi library calls */
static int multi_timer_cb(CURLM *multi, long timeout_ms, struct GlobalInfo *g)
{
(void)multi;
printf("%s %li\n", __PRETTY_FUNCTION__, timeout_ms);
ev_timer_stop(g->loop, &g->timer_event);
if(timeout_ms >= 0) {
/* -1 means delete, other values are timeout times in milliseconds */
double t = timeout_ms / 1000;
ev_timer_init(&g->timer_event, timer_cb, t, 0.);
ev_timer_start(g->loop, &g->timer_event);
}
return 0;
}
/* Die if we get a bad CURLMcode somewhere */
static void mcode_or_die(const char *where, CURLMcode code)
{
if(CURLM_OK != code) {
const char *s;
switch(code) {
case CURLM_BAD_HANDLE:
s = "CURLM_BAD_HANDLE";
break;
case CURLM_BAD_EASY_HANDLE:
s = "CURLM_BAD_EASY_HANDLE";
break;
case CURLM_OUT_OF_MEMORY:
s = "CURLM_OUT_OF_MEMORY";
break;
case CURLM_INTERNAL_ERROR:
s = "CURLM_INTERNAL_ERROR";
break;
case CURLM_UNKNOWN_OPTION:
s = "CURLM_UNKNOWN_OPTION";
break;
case CURLM_LAST:
s = "CURLM_LAST";
break;
default:
s = "CURLM_unknown";
break;
case CURLM_BAD_SOCKET:
s = "CURLM_BAD_SOCKET";
fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
/* ignore this error */
return;
}
fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
exit(code);
}
}
/* Check for completed transfers, and remove their easy handles */
static void check_multi_info(struct GlobalInfo *g)
{
char *eff_url;
CURLMsg *msg;
int msgs_left;
struct ConnInfo *conn;
fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running);
while((msg = curl_multi_info_read(g->multi, &msgs_left))) {
if(msg->msg == CURLMSG_DONE) {
CURL *curl = msg->easy_handle;
CURLcode res = msg->data.result;
curl_easy_getinfo(curl, CURLINFO_PRIVATE, &conn);
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &eff_url);
fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error);
curl_multi_remove_handle(g->multi, curl);
free(conn->url);
curl_easy_cleanup(curl);
free(conn);
}
}
}
/* Called by libevent when we get action on a multi socket */
static void event_cb(EV_P_ struct ev_io *w, int revents)
{
struct GlobalInfo *g;
CURLMcode rc;
int action;
printf("%s w %p revents %i\n", __PRETTY_FUNCTION__, (void *)w, revents);
g = (struct GlobalInfo*) w->data;
action = ((revents & EV_READ) ? CURL_POLL_IN : 0) |
((revents & EV_WRITE) ? CURL_POLL_OUT : 0);
rc = curl_multi_socket_action(g->multi, w->fd, action, &g->still_running);
mcode_or_die("event_cb: curl_multi_socket_action", rc);
check_multi_info(g);
if(g->still_running <= 0) {
fprintf(MSG_OUT, "last transfer done, kill timeout\n");
ev_timer_stop(g->loop, &g->timer_event);
}
}
/* Called by libevent when our timeout expires */
static void timer_cb(EV_P_ struct ev_timer *w, int revents)
{
struct GlobalInfo *g;
CURLMcode rc;
printf("%s w %p revents %i\n", __PRETTY_FUNCTION__, (void *)w, revents);
g = (struct GlobalInfo *)w->data;
rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0,
&g->still_running);
mcode_or_die("timer_cb: curl_multi_socket_action", rc);
check_multi_info(g);
}
/* Clean up the SockInfo structure */
static void remsock(struct SockInfo *f, struct GlobalInfo *g)
{
printf("%s \n", __PRETTY_FUNCTION__);
if(f) {
if(f->evset)
ev_io_stop(g->loop, &f->ev);
free(f);
}
}
/* Assign information to a SockInfo structure */
static void setsock(struct SockInfo *f, curl_socket_t s, CURL *e, int act,
struct GlobalInfo *g)
{
int kind = ((act & CURL_POLL_IN) ? EV_READ : 0) |
((act & CURL_POLL_OUT) ? EV_WRITE : 0);
printf("%s \n", __PRETTY_FUNCTION__);
f->sockfd = s;
f->action = act;
f->curl = e;
if(f->evset)
ev_io_stop(g->loop, &f->ev);
ev_io_init(&f->ev, event_cb, f->sockfd, kind);
f->ev.data = g;
f->evset = 1;
ev_io_start(g->loop, &f->ev);
}
/* Initialize a new SockInfo structure */
static void addsock(curl_socket_t s, CURL *curl, int action,
struct GlobalInfo *g)
{
struct SockInfo *fdp = calloc(1, sizeof(struct SockInfo));
fdp->global = g;
setsock(fdp, s, curl, action, g);
curl_multi_assign(g->multi, s, fdp);
}
/* CURLMOPT_SOCKETFUNCTION */
static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
{
struct GlobalInfo *g = (struct GlobalInfo*) cbp;
struct SockInfo *fdp = (struct SockInfo*) sockp;
const char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE"};
printf("%s e %p s %i what %i cbp %p sockp %p\n",
__PRETTY_FUNCTION__, e, s, what, cbp, sockp);
fprintf(MSG_OUT,
"socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
if(what == CURL_POLL_REMOVE) {
fprintf(MSG_OUT, "\n");
remsock(fdp, g);
}
else {
if(!fdp) {
fprintf(MSG_OUT, "Adding data: %s\n", whatstr[what]);
addsock(s, e, what, g);
}
else {
fprintf(MSG_OUT,
"Changing action from %s to %s\n",
whatstr[fdp->action], whatstr[what]);
setsock(fdp, s, e, what, g);
}
}
return 0;
}
/* CURLOPT_WRITEFUNCTION */
static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
{
size_t realsize = size * nmemb;
struct ConnInfo *conn = (struct ConnInfo*) data;
(void)ptr;
(void)conn;
return realsize;
}
/* CURLOPT_XFERINFOFUNCTION */
static int xferinfo_cb(void *p, curl_off_t dltotal, curl_off_t dlnow,
curl_off_t ult, curl_off_t uln)
{
struct ConnInfo *conn = (struct ConnInfo *)p;
(void)ult;
(void)uln;
fprintf(MSG_OUT, "Progress: %s (%" CURL_FORMAT_CURL_OFF_T
"/%" CURL_FORMAT_CURL_OFF_T ")\n", conn->url, dlnow, dltotal);
return 0;
}
/* Create a new easy handle, and add it to the global curl_multi */
static void new_conn(const char *url, struct GlobalInfo *g)
{
struct ConnInfo *conn;
CURLMcode rc;
conn = calloc(1, sizeof(*conn));
conn->error[0]='\0';
conn->curl = curl_easy_init();
if(!conn->curl) {
fprintf(MSG_OUT, "curl_easy_init() failed, exiting!\n");
exit(2);
}
conn->global = g;
conn->url = strdup(url);
curl_easy_setopt(conn->curl, CURLOPT_URL, conn->url);
curl_easy_setopt(conn->curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(conn->curl, CURLOPT_WRITEDATA, conn);
curl_easy_setopt(conn->curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(conn->curl, CURLOPT_ERRORBUFFER, conn->error);
curl_easy_setopt(conn->curl, CURLOPT_PRIVATE, conn);
curl_easy_setopt(conn->curl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(conn->curl, CURLOPT_XFERINFOFUNCTION, xferinfo_cb);
curl_easy_setopt(conn->curl, CURLOPT_PROGRESSDATA, conn);
curl_easy_setopt(conn->curl, CURLOPT_LOW_SPEED_TIME, 3L);
curl_easy_setopt(conn->curl, CURLOPT_LOW_SPEED_LIMIT, 10L);
fprintf(MSG_OUT,
"Adding easy %p to multi %p (%s)\n", conn->curl, g->multi, url);
rc = curl_multi_add_handle(g->multi, conn->curl);
mcode_or_die("new_conn: curl_multi_add_handle", rc);
/* note that add_handle() sets a timeout to trigger soon so that the
necessary socket_action() gets called */
}
/* This gets called whenever data is received from the fifo */
static void fifo_cb(EV_P_ struct ev_io *w, int revents)
{
char s[1024];
long int rv = 0;
int n = 0;
struct GlobalInfo *g = (struct GlobalInfo *)w->data;
(void)revents;
do {
s[0]='\0';
rv = fscanf(g->input, "%1023s%n", s, &n);
s[n]='\0';
if(n && s[0]) {
new_conn(s, g); /* if we read a URL, go get it! */
}
else
break;
} while(rv != EOF);
}
/* Create a named pipe and tell libevent to monitor it */
static int init_fifo(struct GlobalInfo *g)
{
struct stat st;
static const char *fifo = "hiper.fifo";
curl_socket_t sockfd;
fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
if(lstat(fifo, &st) == 0) {
if((st.st_mode & S_IFMT) == S_IFREG) {
errno = EEXIST;
perror("lstat");
return 1;
}
}
unlink(fifo);
if(mkfifo(fifo, 0600) == -1) {
perror("mkfifo");
return 1;
}
sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0);
if(sockfd == -1) {
perror("open");
return 1;
}
g->input = fdopen(sockfd, "r");
fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo);
ev_io_init(&g->fifo_event, fifo_cb, sockfd, EV_READ);
ev_io_start(g->loop, &g->fifo_event);
return 0;
}
int main(int argc, char **argv)
{
CURLcode res;
struct GlobalInfo g;
(void)argc;
(void)argv;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
memset(&g, 0, sizeof(g));
g.loop = ev_default_loop(0);
if(init_fifo(&g))
return 1;
g.multi = curl_multi_init();
ev_timer_init(&g.timer_event, timer_cb, 0., 0.);
g.timer_event.data = &g;
g.fifo_event.data = &g;
curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g);
curl_multi_setopt(g.multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
curl_multi_setopt(g.multi, CURLMOPT_TIMERDATA, &g);
/* we do not call any curl_multi_socket*() function yet as we have no handles
added! */
ev_loop(g.loop, 0);
curl_multi_cleanup(g.multi);
curl_global_cleanup();
return 0;
}
+176
View File
@@ -0,0 +1,176 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Pass in a custom socket for libcurl to use.
* </DESC>
*/
#ifdef _MSC_VER
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS /* for inet_addr() */
#endif
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
#ifdef _WIN32
#define close closesocket
#else
#include <sys/types.h> /* socket types */
#include <sys/socket.h> /* socket definitions */
#include <netinet/in.h>
#include <arpa/inet.h> /* inet (3) functions */
#include <unistd.h> /* misc. Unix functions */
#endif
#ifdef UNDER_CE
#define strerror(e) "?"
#else
#include <errno.h>
#endif
/* The IP address and port number to connect to */
#define IPADDR "127.0.0.1"
#define PORTNUM 80
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif
static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}
static int closecb(void *clientp, curl_socket_t item)
{
(void)clientp;
printf("libcurl wants to close %d now\n", (int)item);
return 0;
}
static curl_socket_t opensocket(void *clientp,
curlsocktype purpose,
struct curl_sockaddr *address)
{
curl_socket_t sockfd;
(void)purpose;
(void)address;
sockfd = *(curl_socket_t *)clientp;
/* the actual externally set socket is passed in via the OPENSOCKETDATA
option */
return sockfd;
}
static int sockopt_callback(void *clientp, curl_socket_t curlfd,
curlsocktype purpose)
{
(void)clientp;
(void)curlfd;
(void)purpose;
/* This return code was added in libcurl 7.21.5 */
return CURL_SOCKOPT_ALREADY_CONNECTED;
}
int main(void)
{
CURL *curl;
CURLcode res;
struct sockaddr_in servaddr; /* socket address structure */
curl_socket_t sockfd;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/*
* Note that libcurl internally thinks that you connect to the host and
* port that you specify in the URL option.
*/
curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
/* Create the socket "manually" */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == CURL_SOCKET_BAD) {
printf("Error creating listening socket.\n");
return 3;
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORTNUM);
servaddr.sin_addr.s_addr = inet_addr(IPADDR);
if(INADDR_NONE == servaddr.sin_addr.s_addr) {
close(sockfd);
return 2;
}
if(connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) ==
-1) {
close(sockfd);
printf("client error: connect: %s\n", strerror(errno));
return 1;
}
/* no progress meter please */
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
/* send all data to this function */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
/* call this function to get a socket */
curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
/* call this function to close sockets */
curl_easy_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, closecb);
curl_easy_setopt(curl, CURLOPT_CLOSESOCKETDATA, &sockfd);
/* call this function to set options for the socket */
curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
close(sockfd);
if(res) {
printf("libcurl error: %d\n", res);
return 4;
}
}
curl_global_cleanup();
return 0;
}
+114
View File
@@ -0,0 +1,114 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Upload to a file:// URL
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef _WIN32
#undef stat
#define stat _stat
#undef fstat
#define fstat _fstat
#define fileno _fileno
#endif
int main(void)
{
CURL *curl;
CURLcode res;
struct stat file_info;
curl_off_t speed_upload, total_time;
FILE *fd;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
fd = fopen("debugit", "rb"); /* open file to upload */
if(!fd) {
curl_global_cleanup();
return 1; /* cannot continue */
}
/* to get the file size */
#ifdef UNDER_CE
/* !checksrc! disable BANNEDFUNC 1 */
if(stat("debugit", &file_info) != 0) {
#else
if(fstat(fileno(fd), &file_info) != 0) {
#endif
fclose(fd);
curl_global_cleanup();
return 1; /* cannot continue */
}
curl = curl_easy_init();
if(curl) {
/* upload to this place */
curl_easy_setopt(curl, CURLOPT_URL,
"file:///home/dast/src/curl/debug/new");
/* tell it to "upload" to the URL */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* set where to read from (on Windows you need to use READFUNCTION too) */
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
/* and give the size of the upload (optional) */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)file_info.st_size);
/* enable verbose for easier tracing */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
else {
/* now extract transfer info */
curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD_T, &speed_upload);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &total_time);
fprintf(stderr, "Speed: %" CURL_FORMAT_CURL_OFF_T " bytes/sec during "
"%" CURL_FORMAT_CURL_OFF_T
".%06" CURL_FORMAT_CURL_OFF_T " seconds\n",
speed_upload,
total_time / 1000000,
total_time % 1000000);
}
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(fd);
curl_global_cleanup();
return 0;
}
+85
View File
@@ -0,0 +1,85 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include <stdio.h>
#include <curl/curl.h>
/* <DESC>
* Delete a single file from an FTP server.
* </DESC>
*/
static size_t write_cb(void *buffer, size_t size, size_t nmemb, void *stream)
{
(void)buffer;
(void)stream;
return size * nmemb;
}
int main(void)
{
CURL *curl;
CURLcode res;
struct curl_slist *headerlist = NULL;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/*
* You better replace the URL with one that works!
*/
curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp.example.com/");
/* Define our callback to get called when there is data to be written */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
/* Switch on full protocol/debug output */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* build a list of commands to pass to libcurl */
headerlist = curl_slist_append(headerlist, "DELE file-to-remove");
/* pass in list of FTP commands to run after the transfer */
curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
/* clean up the FTP commands list */
curl_slist_free_all(headerlist);
if(CURLE_OK != res) {
/* we failed */
fprintf(stderr, "curl told us %d\n", res);
}
}
curl_global_cleanup();
return (int)res;
}
+144
View File
@@ -0,0 +1,144 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* FTP wildcard pattern matching
* </DESC>
*/
#include <curl/curl.h>
#include <stdio.h>
struct callback_data {
FILE *output;
};
static long file_is_coming(struct curl_fileinfo *finfo, void *input,
int remains)
{
struct callback_data *data = input;
printf("%3d %40s %10luB ", remains, finfo->filename,
(unsigned long)finfo->size);
switch(finfo->filetype) {
case CURLFILETYPE_DIRECTORY:
printf(" DIR\n");
break;
case CURLFILETYPE_FILE:
printf("FILE ");
break;
default:
printf("OTHER\n");
break;
}
if(finfo->filetype == CURLFILETYPE_FILE) {
/* do not transfer files >= 50B */
if(finfo->size > 50) {
printf("SKIPPED\n");
return CURL_CHUNK_BGN_FUNC_SKIP;
}
data->output = fopen(finfo->filename, "wb");
if(!data->output) {
return CURL_CHUNK_BGN_FUNC_FAIL;
}
}
return CURL_CHUNK_BGN_FUNC_OK;
}
static long file_is_downloaded(void *input)
{
struct callback_data *data = input;
if(data->output) {
printf("DOWNLOADED\n");
fclose(data->output);
data->output = 0x0;
}
return CURL_CHUNK_END_FUNC_OK;
}
static size_t write_cb(char *buff, size_t size, size_t nmemb,
void *cb_data)
{
struct callback_data *data = cb_data;
size_t written = 0;
if(data->output)
written = fwrite(buff, size, nmemb, data->output);
else
/* listing output */
written = fwrite(buff, size, nmemb, stdout);
return written;
}
int main(int argc, char **argv)
{
/* curl easy handle */
CURL *curl;
/* help data */
struct callback_data data = { 0 };
/* global initialization */
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
/* initialization of easy handle */
curl = curl_easy_init();
if(!curl) {
curl_global_cleanup();
return CURLE_OUT_OF_MEMORY;
}
/* turn on wildcard matching */
curl_easy_setopt(curl, CURLOPT_WILDCARDMATCH, 1L);
/* callback is called before download of concrete file started */
curl_easy_setopt(curl, CURLOPT_CHUNK_BGN_FUNCTION, file_is_coming);
/* callback is called after data from the file have been transferred */
curl_easy_setopt(curl, CURLOPT_CHUNK_END_FUNCTION, file_is_downloaded);
/* this callback writes contents into files */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
/* put transfer data into callbacks */
curl_easy_setopt(curl, CURLOPT_CHUNK_DATA, &data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
/* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); */
/* set a URL containing wildcard pattern (only in the last part) */
if(argc == 2)
curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
else
curl_easy_setopt(curl, CURLOPT_URL, "ftp://example.com/test/*");
/* and start transfer! */
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_global_cleanup();
return (int)res;
}
+96
View File
@@ -0,0 +1,96 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include <stdio.h>
#include <curl/curl.h>
/* <DESC>
* Get a single file from an FTP server.
* </DESC>
*/
struct FtpFile {
const char *filename;
FILE *stream;
};
static size_t write_cb(void *buffer, size_t size, size_t nmemb, void *stream)
{
struct FtpFile *out = (struct FtpFile *)stream;
if(!out->stream) {
/* open file for writing */
out->stream = fopen(out->filename, "wb");
if(!out->stream)
return 0; /* failure, cannot open file to write */
}
return fwrite(buffer, size, nmemb, out->stream);
}
int main(void)
{
CURL *curl;
CURLcode res;
struct FtpFile ftpfile = {
"curl.tar.gz", /* name to store the file as if successful */
NULL
};
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/*
* You better replace the URL with one that works!
*/
curl_easy_setopt(curl, CURLOPT_URL,
"ftp://ftp.example.com/curl/curl-7.9.2.tar.gz");
/* Define our callback to get called when there is data to be written */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
/* Set a pointer to our struct to pass to the callback */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
/* Switch on full protocol/debug output */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
if(CURLE_OK != res) {
/* we failed */
fprintf(stderr, "curl told us %d\n", res);
}
}
if(ftpfile.stream)
fclose(ftpfile.stream); /* close the local file */
curl_global_cleanup();
return (int)res;
}
+95
View File
@@ -0,0 +1,95 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
/* <DESC>
* Checks a single file's size and mtime from an FTP server.
* </DESC>
*/
static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data)
{
(void)ptr;
(void)data;
/* we are not interested in the headers itself,
so we only return the size we would have saved ... */
return (size_t)(size * nmemb);
}
int main(void)
{
char ftpurl[] = "ftp://ftp.example.com/gnu/binutils/binutils-2.19.1.tar.bz2";
CURL *curl;
CURLcode res;
long filetime = -1;
curl_off_t filesize = 0;
const char *filename = strrchr(ftpurl, '/') + 1;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, ftpurl);
/* No download if the file */
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
/* Ask for filetime */
curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, throw_away);
curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
/* Switch on full protocol/debug output */
/* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); */
res = curl_easy_perform(curl);
if(CURLE_OK == res) {
/* https://curl.se/libcurl/c/curl_easy_getinfo.html */
res = curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
if((CURLE_OK == res) && (filetime >= 0)) {
time_t file_time = (time_t)filetime;
printf("filetime %s: %s", filename, ctime(&file_time));
}
res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
&filesize);
if((CURLE_OK == res) && (filesize > 0))
printf("filesize %s: %" CURL_FORMAT_CURL_OFF_T " bytes\n",
filename, filesize);
}
else {
/* we failed */
fprintf(stderr, "curl told us %d\n", res);
}
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+93
View File
@@ -0,0 +1,93 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include <stdio.h>
#include <curl/curl.h>
/* <DESC>
* Similar to ftpget.c but also stores the received response-lines
* in a separate file using our own callback!
* </DESC>
*/
static size_t write_response(void *ptr, size_t size, size_t nmemb, void *data)
{
FILE *writehere = (FILE *)data;
return fwrite(ptr, size, nmemb, writehere);
}
#define FTPBODY "ftp-list"
#define FTPHEADERS "ftp-responses"
int main(void)
{
CURL *curl;
CURLcode res;
FILE *ftpfile;
FILE *respfile;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
/* local filename to store the file as */
ftpfile = fopen(FTPBODY, "wb"); /* b is binary, needed on Windows */
if(!ftpfile) {
curl_global_cleanup();
return 1;
}
/* local filename to store the FTP server's response lines in */
respfile = fopen(FTPHEADERS, "wb"); /* b is binary, needed on Windows */
if(!respfile) {
fclose(ftpfile);
curl_global_cleanup();
return 1;
}
curl = curl_easy_init();
if(curl) {
/* Get a file listing from sunet */
curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp.example.com/");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftpfile);
/* If you intend to use this on Windows with a libcurl DLL, you must use
CURLOPT_WRITEFUNCTION as well */
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_response);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, respfile);
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(ftpfile); /* close the local file */
fclose(respfile); /* close the response file */
curl_global_cleanup();
return (int)res;
}
+103
View File
@@ -0,0 +1,103 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include <stdio.h>
#include <curl/curl.h>
/* <DESC>
* Get a single file from an FTPS server.
* </DESC>
*/
struct FtpFile {
const char *filename;
FILE *stream;
};
static size_t write_cb(void *buffer, size_t size, size_t nmemb,
void *stream)
{
struct FtpFile *out = (struct FtpFile *)stream;
if(!out->stream) {
/* open file for writing */
out->stream = fopen(out->filename, "wb");
if(!out->stream)
return 0; /* failure, cannot open file to write */
}
return fwrite(buffer, size, nmemb, out->stream);
}
int main(void)
{
CURL *curl;
CURLcode res;
struct FtpFile ftpfile = {
"yourfile.bin", /* name to store the file as if successful */
NULL
};
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/*
* You better replace the URL with one that works! Note that we use an
* FTP:// URL with standard explicit FTPS. You can also do FTPS:// URLs if
* you want to do the rarer kind of transfers: implicit.
*/
curl_easy_setopt(curl, CURLOPT_URL,
"ftp://user@server/home/user/file.txt");
/* Define our callback to get called when there is data to be written */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
/* Set a pointer to our struct to pass to the callback */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
/* We activate SSL and we require it for both control and data */
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
/* Switch on full protocol/debug output */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
if(CURLE_OK != res) {
/* we failed */
fprintf(stderr, "curl told us %d\n", res);
}
}
if(ftpfile.stream)
fclose(ftpfile.stream); /* close the local file */
curl_global_cleanup();
return (int)res;
}
+163
View File
@@ -0,0 +1,163 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef UNDER_CE
#define strerror(e) "?"
#else
#include <errno.h>
#endif
#ifdef _WIN32
#include <io.h>
#undef stat
#define stat _stat
#undef fstat
#define fstat _fstat
#define fileno _fileno
#else
#include <unistd.h>
#endif
/* <DESC>
* Performs an FTP upload and renames the file just after a successful
* transfer.
* </DESC>
*/
#define LOCAL_FILE "/tmp/uploadthis.txt"
#define UPLOAD_FILE_AS "while-uploading.txt"
#define REMOTE_URL "ftp://example.com/" UPLOAD_FILE_AS
#define RENAME_FILE_TO "renamed-and-fine.txt"
/* NOTE: if you want this example to work on Windows with libcurl as a DLL,
you MUST also provide a read callback with CURLOPT_READFUNCTION. Failing to
do so might give you a crash since a DLL may not use the variable's memory
when passed in to it from an app like this. */
static size_t read_cb(char *ptr, size_t size, size_t nmemb, void *stream)
{
unsigned long nread;
/* in real-world cases, this would probably get this data differently
as this fread() stuff is exactly what the library already would do
by default internally */
size_t retcode = fread(ptr, size, nmemb, stream);
if(retcode > 0) {
nread = (unsigned long)retcode;
fprintf(stderr, "*** We read %lu bytes from file\n", nread);
}
return retcode;
}
int main(void)
{
CURL *curl;
CURLcode res;
FILE *hd_src;
struct stat file_info;
curl_off_t fsize;
struct curl_slist *headerlist = NULL;
static const char buf_1[] = "RNFR " UPLOAD_FILE_AS;
static const char buf_2[] = "RNTO " RENAME_FILE_TO;
/* get a FILE * of the file */
hd_src = fopen(LOCAL_FILE, "rb");
if(!hd_src) {
printf("Couldn't open '%s': %s\n", LOCAL_FILE, strerror(errno));
return 2;
}
/* to get the file size */
#ifdef UNDER_CE
/* !checksrc! disable BANNEDFUNC 1 */
if(stat(LOCAL_FILE, &file_info) != 0) {
#else
if(fstat(fileno(hd_src), &file_info) != 0) {
#endif
fclose(hd_src);
return 1; /* cannot continue */
}
fsize = file_info.st_size;
printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
/* In Windows, this inits the Winsock stuff */
res = curl_global_init(CURL_GLOBAL_ALL);
if(res) {
fclose(hd_src);
return (int)res;
}
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
/* build a list of commands to pass to libcurl */
headerlist = curl_slist_append(headerlist, buf_1);
headerlist = curl_slist_append(headerlist, buf_2);
/* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb);
/* enable uploading */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* specify target */
curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL);
/* pass in that last of FTP commands to run after the transfer */
curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
/* now specify which file to upload */
curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
/* Set the size of the file to upload (optional). If you give a *_LARGE
option you MUST make sure that the type of the passed-in argument is a
curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
make sure that to pass in a type 'long' argument. */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, fsize);
/* Now run off and do what you have been told! */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* clean up the FTP commands list */
curl_slist_free_all(headerlist);
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(hd_src); /* close the local file */
curl_global_cleanup();
return (int)res;
}
+126
View File
@@ -0,0 +1,126 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* FTP upload a file from memory
* </DESC>
*/
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
static const char data[]=
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
"___ rhoncus odio id venenatis volutpat. Vestibulum dapibus "
"bibendum ullamcorper. Maecenas finibus elit augue, vel "
"condimentum odio maximus nec. In hac habitasse platea dictumst. "
"Vestibulum vel dolor et turpis rutrum finibus ac at nulla. "
"Vivamus nec neque ac elit blandit pretium vitae maximus ipsum. "
"Quisque sodales magna vel erat auctor, sed pellentesque nisi "
"rhoncus. Donec vehicula maximus pretium. Aliquam eu tincidunt "
"lorem.";
struct WriteThis {
const char *readptr;
size_t sizeleft;
};
static size_t read_cb(char *ptr, size_t size, size_t nmemb, void *userp)
{
struct WriteThis *upload = (struct WriteThis *)userp;
size_t max = size*nmemb;
if(max < 1)
return 0;
if(upload->sizeleft) {
size_t copylen = max;
if(copylen > upload->sizeleft)
copylen = upload->sizeleft;
memcpy(ptr, upload->readptr, copylen);
upload->readptr += copylen;
upload->sizeleft -= copylen;
return copylen;
}
return 0; /* no more data left to deliver */
}
int main(void)
{
CURL *curl;
CURLcode res;
struct WriteThis upload;
upload.readptr = data;
upload.sizeleft = strlen(data);
/* In Windows, this inits the Winsock stuff */
res = curl_global_init(CURL_GLOBAL_DEFAULT);
/* Check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_global_init() failed: %s\n",
curl_easy_strerror(res));
return 1;
}
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
/* First set the URL, the target file */
curl_easy_setopt(curl, CURLOPT_URL,
"ftp://example.com/path/to/upload/file");
/* User and password for the FTP login */
curl_easy_setopt(curl, CURLOPT_USERPWD, "login:secret");
/* Now specify we want to UPLOAD data */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb);
/* pointer to pass to our read function */
curl_easy_setopt(curl, CURLOPT_READDATA, &upload);
/* get verbose debug output please */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* Set the expected upload size. */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)upload.sizeleft);
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+169
View File
@@ -0,0 +1,169 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Upload to FTP, resuming failed transfers. Active mode.
* </DESC>
*/
#include <stdlib.h>
#include <stdio.h>
#include <curl/curl.h>
/* parse headers for Content-Length */
static size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb,
void *stream)
{
int r;
long len = 0;
r = sscanf(ptr, "Content-Length: %ld\n", &len);
if(r == 1)
*((long *) stream) = len;
return size * nmemb;
}
/* discard downloaded data */
static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *stream)
{
(void)ptr;
(void)stream;
return size * nmemb;
}
/* read data to upload */
static size_t read_cb(char *ptr, size_t size, size_t nmemb, void *stream)
{
FILE *f = stream;
size_t n;
if(ferror(f))
return CURL_READFUNC_ABORT;
n = fread(ptr, size, nmemb, f) * size;
return n;
}
static int upload(CURL *curl, const char *remotepath,
const char *localpath, long timeout, long tries)
{
FILE *f;
long uploaded_len = 0;
CURLcode res = CURLE_GOT_NOTHING;
int c;
f = fopen(localpath, "rb");
if(!f) {
#ifndef UNDER_CE
perror(NULL);
#endif
return 0;
}
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_URL, remotepath);
if(timeout)
curl_easy_setopt(curl, CURLOPT_SERVER_RESPONSE_TIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &uploaded_len);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb);
curl_easy_setopt(curl, CURLOPT_READDATA, f);
/* enable active mode */
curl_easy_setopt(curl, CURLOPT_FTPPORT, "-");
/* allow the server no more than 7 seconds to connect back */
curl_easy_setopt(curl, CURLOPT_ACCEPTTIMEOUT_MS, 7000L);
curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
for(c = 0; (res != CURLE_OK) && (c < tries); c++) {
/* are we resuming? */
if(c) { /* yes */
/* determine the length of the file already written */
/*
* With NOBODY and NOHEADER, libcurl issues a SIZE command, but the only
* way to retrieve the result is to parse the returned Content-Length
* header. Thus, getcontentlengthfunc(). We need write_cb() above
* because HEADER dumps the headers to stdout without it.
*/
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
continue;
curl_easy_setopt(curl, CURLOPT_NOBODY, 0L);
curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
fseek(f, uploaded_len, SEEK_SET);
curl_easy_setopt(curl, CURLOPT_APPEND, 1L);
}
else { /* no */
curl_easy_setopt(curl, CURLOPT_APPEND, 0L);
}
res = curl_easy_perform(curl);
}
fclose(f);
if(res == CURLE_OK)
return 1;
else {
fprintf(stderr, "%s\n", curl_easy_strerror(res));
return 0;
}
}
int main(void)
{
CURL *curl = NULL;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
upload(curl, "ftp://user:pass@example.com/path/file", "C:\\file", 0, 3);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
+58
View File
@@ -0,0 +1,58 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Use getinfo to get content-type after completed transfer.
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
res = curl_easy_perform(curl);
if(CURLE_OK == res) {
char *ct;
/* ask for the content-type */
res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
if((CURLE_OK == res) && ct)
printf("We received Content-Type: %s\n", ct);
}
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+122
View File
@@ -0,0 +1,122 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Shows how the write callback function can be used to download data into a
* chunk of memory instead of storing it in a file.
* </DESC>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t write_cb(void *contents, size_t size, size_t nmemb,
void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
char *ptr = realloc(mem->memory, mem->size + realsize + 1);
if(!ptr) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
int main(void)
{
CURL *curl;
CURLcode res;
struct MemoryStruct chunk;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
chunk.memory = malloc(1); /* grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
/* init the curl session */
curl = curl_easy_init();
if(curl) {
/* specify URL to get */
curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
/* send all data to this function */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
/* some servers do not like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
/* get it! */
res = curl_easy_perform(curl);
/* check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
else {
/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.
*
* Do something nice with it!
*/
printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
}
/* cleanup curl stuff */
curl_easy_cleanup(curl);
}
free(chunk.memory);
/* we are done with libcurl, so clean it up */
curl_global_cleanup();
return (int)res;
}
+77
View File
@@ -0,0 +1,77 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Show how to extract Location: header and URL to redirect to.
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
char *location;
long response_code;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
/* example.com is redirected, figure out the redirection! */
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
else {
res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
if((res == CURLE_OK) &&
((response_code / 100) != 3)) {
/* a redirect implies a 3xx response code */
fprintf(stderr, "Not a redirect.\n");
}
else {
res = curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL, &location);
if((res == CURLE_OK) && location) {
/* This is the new absolute URL that you could redirect to, even if
* the Location: response header may have been a relative URL. */
printf("Redirected to: %s\n", location);
}
}
}
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+62
View File
@@ -0,0 +1,62 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Show how to extract referrer header.
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
curl_easy_setopt(curl, CURLOPT_REFERER, "https://example.org/referrer");
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
else {
char *hdr;
res = curl_easy_getinfo(curl, CURLINFO_REFERER, &hdr);
if((res == CURLE_OK) && hdr)
printf("Referrer header: %s\n", hdr);
}
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
+451
View File
@@ -0,0 +1,451 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* multi socket API usage together with glib2
* </DESC>
*/
/* Example application source code using the multi socket interface to
* download many files at once.
*
* Written by Jeff Pohlmeyer
Requires glib-2.x and a (POSIX?) system that has mkfifo().
This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c"
sample programs, adapted to use glib's g_io_channel in place of libevent.
When running, the program creates the named pipe "hiper.fifo"
Whenever there is input into the fifo, the program reads the input as a list
of URL's and creates some new easy handles to fetch each URL via the
curl_multi "hiper" API.
Thus, you can try a single URL:
% echo http://www.yahoo.com > hiper.fifo
Or a whole bunch of them:
% cat my-url-list > hiper.fifo
The fifo buffer is handled almost instantly, so you can even add more URL's
while the previous requests are still being downloaded.
This is purely a demo app, all retrieved data is simply discarded by the write
callback.
*/
#include <glib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <curl/curl.h>
#define MSG_OUT g_print /* Change to "g_error" to write to stderr */
#define SHOW_VERBOSE 0L /* Set to non-zero for libcurl messages */
#define SHOW_PROGRESS 0 /* Set to non-zero to enable progress callback */
/* Global information, common to all connections */
struct GlobalInfo {
CURLM *multi;
guint timer_event;
int still_running;
};
/* Information associated with a specific easy handle */
struct ConnInfo {
CURL *curl;
char *url;
struct GlobalInfo *global;
char error[CURL_ERROR_SIZE];
};
/* Information associated with a specific socket */
struct SockInfo {
curl_socket_t sockfd;
CURL *curl;
int action;
long timeout;
GIOChannel *ch;
guint ev;
struct GlobalInfo *global;
};
/* Die if we get a bad CURLMcode somewhere */
static void mcode_or_die(const char *where, CURLMcode code)
{
if(CURLM_OK != code) {
const char *s;
switch(code) {
case CURLM_BAD_HANDLE: s = "CURLM_BAD_HANDLE"; break;
case CURLM_BAD_EASY_HANDLE: s = "CURLM_BAD_EASY_HANDLE"; break;
case CURLM_OUT_OF_MEMORY: s = "CURLM_OUT_OF_MEMORY"; break;
case CURLM_INTERNAL_ERROR: s = "CURLM_INTERNAL_ERROR"; break;
case CURLM_BAD_SOCKET: s = "CURLM_BAD_SOCKET"; break;
case CURLM_UNKNOWN_OPTION: s = "CURLM_UNKNOWN_OPTION"; break;
case CURLM_LAST: s = "CURLM_LAST"; break;
default: s = "CURLM_unknown";
}
MSG_OUT("ERROR: %s returns %s\n", where, s);
exit(code);
}
}
/* Check for completed transfers, and remove their easy handles */
static void check_multi_info(struct GlobalInfo *g)
{
CURLMsg *msg;
int msgs_left;
MSG_OUT("REMAINING: %d\n", g->still_running);
while((msg = curl_multi_info_read(g->multi, &msgs_left))) {
if(msg->msg == CURLMSG_DONE) {
CURL *curl = msg->easy_handle;
CURLcode res = msg->data.result;
char *eff_url;
struct ConnInfo *conn;
curl_easy_getinfo(curl, CURLINFO_PRIVATE, &conn);
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &eff_url);
MSG_OUT("DONE: %s => (%d) %s\n", eff_url, res, conn->error);
curl_multi_remove_handle(g->multi, curl);
free(conn->url);
curl_easy_cleanup(curl);
free(conn);
}
}
}
/* Called by glib when our timeout expires */
static gboolean timer_cb(gpointer data)
{
struct GlobalInfo *g = (struct GlobalInfo *)data;
CURLMcode rc;
rc = curl_multi_socket_action(g->multi,
CURL_SOCKET_TIMEOUT, 0, &g->still_running);
mcode_or_die("timer_cb: curl_multi_socket_action", rc);
check_multi_info(g);
return FALSE;
}
/* Update the event timer after curl_multi library calls */
static int update_timeout_cb(CURLM *multi, long timeout_ms, void *userp)
{
struct timeval timeout;
struct GlobalInfo *g = (struct GlobalInfo *)userp;
timeout.tv_sec = timeout_ms/1000;
timeout.tv_usec = (timeout_ms%1000)*1000;
MSG_OUT("*** update_timeout_cb %ld => %ld:%ld ***\n",
timeout_ms, timeout.tv_sec, timeout.tv_usec);
/*
* if timeout_ms is -1, just delete the timer
*
* For other values of timeout_ms, this should set or *update* the timer to
* the new value
*/
if(timeout_ms >= 0)
g->timer_event = g_timeout_add(timeout_ms, timer_cb, g);
return 0;
}
/* Called by glib when we get action on a multi socket */
static gboolean event_cb(GIOChannel *ch, GIOCondition condition, gpointer data)
{
struct GlobalInfo *g = (struct GlobalInfo*) data;
CURLMcode rc;
int fd = g_io_channel_unix_get_fd(ch);
int action =
((condition & G_IO_IN) ? CURL_CSELECT_IN : 0) |
((condition & G_IO_OUT) ? CURL_CSELECT_OUT : 0);
rc = curl_multi_socket_action(g->multi, fd, action, &g->still_running);
mcode_or_die("event_cb: curl_multi_socket_action", rc);
check_multi_info(g);
if(g->still_running) {
return TRUE;
}
else {
MSG_OUT("last transfer done, kill timeout\n");
if(g->timer_event) {
g_source_remove(g->timer_event);
}
return FALSE;
}
}
/* Clean up the SockInfo structure */
static void remsock(struct SockInfo *f)
{
if(!f) {
return;
}
if(f->ev) {
g_source_remove(f->ev);
}
g_free(f);
}
/* Assign information to a SockInfo structure */
static void setsock(struct SockInfo *f, curl_socket_t s, CURL *e, int act,
struct GlobalInfo *g)
{
GIOCondition kind =
((act & CURL_POLL_IN) ? G_IO_IN : 0) |
((act & CURL_POLL_OUT) ? G_IO_OUT : 0);
f->sockfd = s;
f->action = act;
f->curl = e;
if(f->ev) {
g_source_remove(f->ev);
}
f->ev = g_io_add_watch(f->ch, kind, event_cb, g);
}
/* Initialize a new SockInfo structure */
static void addsock(curl_socket_t s, CURL *curl, int action,
struct GlobalInfo *g)
{
struct SockInfo *fdp = g_malloc0(sizeof(struct SockInfo));
fdp->global = g;
fdp->ch = g_io_channel_unix_new(s);
setsock(fdp, s, curl, action, g);
curl_multi_assign(g->multi, s, fdp);
}
/* CURLMOPT_SOCKETFUNCTION */
static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
{
struct GlobalInfo *g = (struct GlobalInfo*) cbp;
struct SockInfo *fdp = (struct SockInfo*) sockp;
static const char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE" };
MSG_OUT("socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
if(what == CURL_POLL_REMOVE) {
MSG_OUT("\n");
remsock(fdp);
}
else {
if(!fdp) {
MSG_OUT("Adding data: %s%s\n",
(what & CURL_POLL_IN) ? "READ" : "",
(what & CURL_POLL_OUT) ? "WRITE" : "");
addsock(s, e, what, g);
}
else {
MSG_OUT(
"Changing action from %d to %d\n", fdp->action, what);
setsock(fdp, s, e, what, g);
}
}
return 0;
}
/* CURLOPT_WRITEFUNCTION */
static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
{
size_t realsize = size * nmemb;
struct ConnInfo *conn = (struct ConnInfo*) data;
(void)ptr;
(void)conn;
return realsize;
}
/* CURLOPT_XFERINFOFUNCTION */
static int xferinfo_cb(void *p, curl_off_t dltotal, curl_off_t dlnow,
curl_off_t ult, curl_off_t uln)
{
struct ConnInfo *conn = (struct ConnInfo *)p;
(void)ult;
(void)uln;
fprintf(MSG_OUT, "Progress: %s (%" CURL_FORMAT_CURL_OFF_T
"/%" CURL_FORMAT_CURL_OFF_T ")\n", conn->url, dlnow, dltotal);
return 0;
}
/* Create a new easy handle, and add it to the global curl_multi */
static void new_conn(const char *url, struct GlobalInfo *g)
{
struct ConnInfo *conn;
CURLMcode rc;
conn = g_malloc0(sizeof(*conn));
conn->error[0] = '\0';
conn->curl = curl_easy_init();
if(!conn->curl) {
MSG_OUT("curl_easy_init() failed, exiting!\n");
exit(2);
}
conn->global = g;
conn->url = g_strdup(url);
curl_easy_setopt(conn->curl, CURLOPT_URL, conn->url);
curl_easy_setopt(conn->curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(conn->curl, CURLOPT_WRITEDATA, &conn);
curl_easy_setopt(conn->curl, CURLOPT_VERBOSE, SHOW_VERBOSE);
curl_easy_setopt(conn->curl, CURLOPT_ERRORBUFFER, conn->error);
curl_easy_setopt(conn->curl, CURLOPT_PRIVATE, conn);
curl_easy_setopt(conn->curl, CURLOPT_NOPROGRESS, SHOW_PROGRESS ? 0L : 1L);
curl_easy_setopt(conn->curl, CURLOPT_XFERINFOFUNCTION, xferinfo_cb);
curl_easy_setopt(conn->curl, CURLOPT_PROGRESSDATA, conn);
curl_easy_setopt(conn->curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(conn->curl, CURLOPT_CONNECTTIMEOUT, 30L);
curl_easy_setopt(conn->curl, CURLOPT_LOW_SPEED_LIMIT, 1L);
curl_easy_setopt(conn->curl, CURLOPT_LOW_SPEED_TIME, 30L);
MSG_OUT("Adding easy %p to multi %p (%s)\n", conn->curl, g->multi, url);
rc = curl_multi_add_handle(g->multi, conn->curl);
mcode_or_die("new_conn: curl_multi_add_handle", rc);
/* note that add_handle() sets a timeout to trigger soon so that the
necessary socket_action() gets called */
}
/* This gets called by glib whenever data is received from the fifo */
static gboolean fifo_cb(GIOChannel *ch, GIOCondition condition, gpointer data)
{
#define BUF_SIZE 1024
gsize len, tp;
gchar *buf, *tmp, *all = NULL;
GIOStatus rv;
do {
GError *err = NULL;
rv = g_io_channel_read_line(ch, &buf, &len, &tp, &err);
if(buf) {
if(tp) {
buf[tp]='\0';
}
new_conn(buf, (struct GlobalInfo*)data);
g_free(buf);
}
else {
buf = g_malloc(BUF_SIZE + 1);
while(TRUE) {
buf[BUF_SIZE]='\0';
g_io_channel_read_chars(ch, buf, BUF_SIZE, &len, &err);
if(len) {
buf[len]='\0';
if(all) {
tmp = all;
all = g_strdup_printf("%s%s", tmp, buf);
g_free(tmp);
}
else {
all = g_strdup(buf);
}
}
else {
break;
}
}
if(all) {
new_conn(all, (struct GlobalInfo*)data);
g_free(all);
}
g_free(buf);
}
if(err) {
g_error("fifo_cb: %s", err->message);
g_free(err);
break;
}
} while((len) && (rv == G_IO_STATUS_NORMAL));
return TRUE;
}
int init_fifo(void)
{
struct stat st;
const char *fifo = "hiper.fifo";
int socket;
if(lstat(fifo, &st) == 0) {
if((st.st_mode & S_IFMT) == S_IFREG) {
errno = EEXIST;
perror("lstat");
return CURL_SOCKET_BAD;
}
}
unlink(fifo);
if(mkfifo(fifo, 0600) == -1) {
perror("mkfifo");
return CURL_SOCKET_BAD;
}
socket = open(fifo, O_RDWR | O_NONBLOCK, 0);
if(socket == CURL_SOCKET_BAD) {
perror("open");
return socket;
}
MSG_OUT("Now, pipe some URL's into > %s\n", fifo);
return socket;
}
int main(void)
{
struct GlobalInfo *g = g_malloc0(sizeof(struct GlobalInfo));
GMainLoop*gmain;
int fd;
GIOChannel* ch;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
fd = init_fifo();
if(fd == CURL_SOCKET_BAD) {
curl_global_cleanup();
return 1;
}
ch = g_io_channel_unix_new(fd);
g_io_add_watch(ch, G_IO_IN, fifo_cb, g);
gmain = g_main_loop_new(NULL, FALSE);
g->multi = curl_multi_init();
if(g->multi) {
curl_multi_setopt(g->multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
curl_multi_setopt(g->multi, CURLMOPT_SOCKETDATA, g);
curl_multi_setopt(g->multi, CURLMOPT_TIMERFUNCTION, update_timeout_cb);
curl_multi_setopt(g->multi, CURLMOPT_TIMERDATA, g);
/* we do not call any curl_multi_socket*() function yet as we have no
handles added! */
g_main_loop_run(gmain);
curl_multi_cleanup(g->multi);
}
curl_global_cleanup();
return 0;
}
+85
View File
@@ -0,0 +1,85 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Extract headers post transfer with the header API
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
static size_t write_cb(char *data, size_t n, size_t l, void *userp)
{
/* take care of the data here, ignored in this example */
(void)data;
(void)userp;
return n*l;
}
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
struct curl_header *header;
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* this example just ignores the content */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
if(CURLHE_OK == curl_easy_header(curl, "Content-Type", 0, CURLH_HEADER,
-1, &header))
printf("Got content-type: %s\n", header->value);
printf("All server headers:\n");
{
struct curl_header *h;
struct curl_header *prev = NULL;
do {
h = curl_easy_nextheader(curl, CURLH_HEADER, -1, prev);
if(h)
printf(" %s: %s (%u)\n", h->name, h->value, (unsigned int)h->amount);
prev = h;
} while(h);
}
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
+453
View File
@@ -0,0 +1,453 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* multi socket API usage with libevent 2
* </DESC>
*/
/* Example application source code using the multi socket interface to
download many files at once.
Written by Jeff Pohlmeyer
Requires libevent version 2 and a (POSIX?) system that has mkfifo().
This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c"
sample programs.
When running, the program creates the named pipe "hiper.fifo"
Whenever there is input into the fifo, the program reads the input as a list
of URL's and creates some new easy handles to fetch each URL via the
curl_multi "hiper" API.
Thus, you can try a single URL:
% echo http://www.yahoo.com > hiper.fifo
Or a whole bunch of them:
% cat my-url-list > hiper.fifo
The fifo buffer is handled almost instantly, so you can even add more URL's
while the previous requests are still being downloaded.
Note:
For the sake of simplicity, URL length is limited to 1023 char's !
This is purely a demo app, all retrieved data is simply discarded by the write
callback.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <sys/poll.h>
#include <curl/curl.h>
#include <event2/event.h>
#include <event2/event_struct.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/cdefs.h>
#define MSG_OUT stdout /* Send info to stdout, change to stderr if you want */
/* Global information, common to all connections */
struct GlobalInfo {
struct event_base *evbase;
struct event fifo_event;
struct event timer_event;
CURLM *multi;
int still_running;
FILE *input;
int stopped;
};
/* Information associated with a specific easy handle */
struct ConnInfo {
CURL *curl;
char *url;
struct GlobalInfo *global;
char error[CURL_ERROR_SIZE];
};
/* Information associated with a specific socket */
struct SockInfo {
curl_socket_t sockfd;
CURL *curl;
int action;
long timeout;
struct event ev;
struct GlobalInfo *global;
};
#define mycase(code) \
case code: s = __STRING(code)
/* Die if we get a bad CURLMcode somewhere */
static void mcode_or_die(const char *where, CURLMcode code)
{
if(CURLM_OK != code) {
const char *s;
switch(code) {
mycase(CURLM_BAD_HANDLE); break;
mycase(CURLM_BAD_EASY_HANDLE); break;
mycase(CURLM_OUT_OF_MEMORY); break;
mycase(CURLM_INTERNAL_ERROR); break;
mycase(CURLM_UNKNOWN_OPTION); break;
mycase(CURLM_LAST); break;
default: s = "CURLM_unknown"; break;
mycase(CURLM_BAD_SOCKET);
fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
/* ignore this error */
return;
}
fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
exit(code);
}
}
/* Update the event timer after curl_multi library calls */
static int multi_timer_cb(CURLM *multi, long timeout_ms, struct GlobalInfo *g)
{
struct timeval timeout;
(void)multi;
timeout.tv_sec = timeout_ms/1000;
timeout.tv_usec = (timeout_ms%1000)*1000;
fprintf(MSG_OUT, "multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms);
/*
* if timeout_ms is -1, just delete the timer
*
* For all other values of timeout_ms, this should set or *update* the timer
* to the new value
*/
if(timeout_ms == -1)
evtimer_del(&g->timer_event);
else /* includes timeout zero */
evtimer_add(&g->timer_event, &timeout);
return 0;
}
/* Check for completed transfers, and remove their easy handles */
static void check_multi_info(struct GlobalInfo *g)
{
char *eff_url;
CURLMsg *msg;
int msgs_left;
struct ConnInfo *conn;
fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running);
while((msg = curl_multi_info_read(g->multi, &msgs_left))) {
if(msg->msg == CURLMSG_DONE) {
CURL *curl = msg->easy_handle;
CURLcode res = msg->data.result;
curl_easy_getinfo(curl, CURLINFO_PRIVATE, &conn);
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &eff_url);
fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error);
curl_multi_remove_handle(g->multi, curl);
free(conn->url);
curl_easy_cleanup(curl);
free(conn);
}
}
if(g->still_running == 0 && g->stopped)
event_base_loopbreak(g->evbase);
}
/* Called by libevent when we get action on a multi socket */
static void event_cb(int fd, short kind, void *userp)
{
struct GlobalInfo *g = (struct GlobalInfo*) userp;
CURLMcode rc;
int action =
((kind & EV_READ) ? CURL_CSELECT_IN : 0) |
((kind & EV_WRITE) ? CURL_CSELECT_OUT : 0);
rc = curl_multi_socket_action(g->multi, fd, action, &g->still_running);
mcode_or_die("event_cb: curl_multi_socket_action", rc);
check_multi_info(g);
if(g->still_running <= 0) {
fprintf(MSG_OUT, "last transfer done, kill timeout\n");
if(evtimer_pending(&g->timer_event, NULL)) {
evtimer_del(&g->timer_event);
}
}
}
/* Called by libevent when our timeout expires */
static void timer_cb(int fd, short kind, void *userp)
{
struct GlobalInfo *g = (struct GlobalInfo *)userp;
CURLMcode rc;
(void)fd;
(void)kind;
rc = curl_multi_socket_action(g->multi,
CURL_SOCKET_TIMEOUT, 0, &g->still_running);
mcode_or_die("timer_cb: curl_multi_socket_action", rc);
check_multi_info(g);
}
/* Clean up the SockInfo structure */
static void remsock(struct SockInfo *f)
{
if(f) {
if(event_initialized(&f->ev)) {
event_del(&f->ev);
}
free(f);
}
}
/* Assign information to a SockInfo structure */
static void setsock(struct SockInfo *f, curl_socket_t s, CURL *e, int act,
struct GlobalInfo *g)
{
int kind =
((act & CURL_POLL_IN) ? EV_READ : 0) |
((act & CURL_POLL_OUT) ? EV_WRITE : 0) | EV_PERSIST;
f->sockfd = s;
f->action = act;
f->curl = e;
if(event_initialized(&f->ev)) {
event_del(&f->ev);
}
event_assign(&f->ev, g->evbase, f->sockfd, (short)kind, event_cb, g);
event_add(&f->ev, NULL);
}
/* Initialize a new SockInfo structure */
static void addsock(curl_socket_t s, CURL *curl, int action,
struct GlobalInfo *g)
{
struct SockInfo *fdp = calloc(1, sizeof(struct SockInfo));
fdp->global = g;
setsock(fdp, s, curl, action, g);
curl_multi_assign(g->multi, s, fdp);
}
/* CURLMOPT_SOCKETFUNCTION */
static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
{
struct GlobalInfo *g = (struct GlobalInfo*) cbp;
struct SockInfo *fdp = (struct SockInfo*) sockp;
const char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE" };
fprintf(MSG_OUT,
"socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
if(what == CURL_POLL_REMOVE) {
fprintf(MSG_OUT, "\n");
remsock(fdp);
}
else {
if(!fdp) {
fprintf(MSG_OUT, "Adding data: %s\n", whatstr[what]);
addsock(s, e, what, g);
}
else {
fprintf(MSG_OUT,
"Changing action from %s to %s\n",
whatstr[fdp->action], whatstr[what]);
setsock(fdp, s, e, what, g);
}
}
return 0;
}
/* CURLOPT_WRITEFUNCTION */
static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
{
(void)ptr;
(void)data;
return size * nmemb;
}
/* CURLOPT_PROGRESSFUNCTION */
static int xferinfo_cb(void *p, curl_off_t dltotal, curl_off_t dlnow,
curl_off_t ult, curl_off_t uln)
{
struct ConnInfo *conn = (struct ConnInfo *)p;
(void)ult;
(void)uln;
fprintf(MSG_OUT, "Progress: %s (%" CURL_FORMAT_CURL_OFF_T
"/%" CURL_FORMAT_CURL_OFF_T ")\n", conn->url, dlnow, dltotal);
return 0;
}
/* Create a new easy handle, and add it to the global curl_multi */
static void new_conn(const char *url, struct GlobalInfo *g)
{
struct ConnInfo *conn;
CURLMcode rc;
conn = calloc(1, sizeof(*conn));
conn->error[0] = '\0';
conn->curl = curl_easy_init();
if(!conn->curl) {
fprintf(MSG_OUT, "curl_easy_init() failed, exiting!\n");
exit(2);
}
conn->global = g;
conn->url = strdup(url);
curl_easy_setopt(conn->curl, CURLOPT_URL, conn->url);
curl_easy_setopt(conn->curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(conn->curl, CURLOPT_WRITEDATA, conn);
curl_easy_setopt(conn->curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(conn->curl, CURLOPT_ERRORBUFFER, conn->error);
curl_easy_setopt(conn->curl, CURLOPT_PRIVATE, conn);
curl_easy_setopt(conn->curl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(conn->curl, CURLOPT_XFERINFOFUNCTION, xferinfo_cb);
curl_easy_setopt(conn->curl, CURLOPT_PROGRESSDATA, conn);
curl_easy_setopt(conn->curl, CURLOPT_FOLLOWLOCATION, 1L);
fprintf(MSG_OUT,
"Adding easy %p to multi %p (%s)\n", conn->curl, g->multi, url);
rc = curl_multi_add_handle(g->multi, conn->curl);
mcode_or_die("new_conn: curl_multi_add_handle", rc);
/* note that the add_handle() sets a time-out to trigger soon so that
the necessary socket_action() gets called */
}
/* This gets called whenever data is received from the fifo */
static void fifo_cb(int fd, short event, void *arg)
{
char s[1024];
long int rv = 0;
int n = 0;
struct GlobalInfo *g = (struct GlobalInfo *)arg;
(void)fd;
(void)event;
do {
s[0]='\0';
rv = fscanf(g->input, "%1023s%n", s, &n);
s[n]='\0';
if(n && s[0]) {
if(!strcmp(s, "stop")) {
g->stopped = 1;
if(g->still_running == 0)
event_base_loopbreak(g->evbase);
}
else
new_conn(s, arg); /* if we read a URL, go get it! */
}
else
break;
} while(rv != EOF);
}
/* Create a named pipe and tell libevent to monitor it */
static const char *fifo = "hiper.fifo";
static int init_fifo(struct GlobalInfo *g)
{
struct stat st;
curl_socket_t sockfd;
fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
if(lstat(fifo, &st) == 0) {
if((st.st_mode & S_IFMT) == S_IFREG) {
errno = EEXIST;
perror("lstat");
return 1;
}
}
unlink(fifo);
if(mkfifo (fifo, 0600) == -1) {
perror("mkfifo");
return 1;
}
sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0);
if(sockfd == -1) {
perror("open");
return 1;
}
g->input = fdopen(sockfd, "r");
fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo);
event_assign(&g->fifo_event, g->evbase, sockfd, EV_READ|EV_PERSIST,
fifo_cb, g);
event_add(&g->fifo_event, NULL);
return 0;
}
static void clean_fifo(struct GlobalInfo *g)
{
event_del(&g->fifo_event);
fclose(g->input);
unlink(fifo);
}
int main(int argc, char **argv)
{
CURLcode res;
struct GlobalInfo g;
(void)argc;
(void)argv;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
memset(&g, 0, sizeof(g));
g.evbase = event_base_new();
if(init_fifo(&g)) {
curl_global_cleanup();
return 1;
}
g.multi = curl_multi_init();
evtimer_assign(&g.timer_event, g.evbase, timer_cb, &g);
/* setup the generic multi interface options we want */
curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g);
curl_multi_setopt(g.multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
curl_multi_setopt(g.multi, CURLMOPT_TIMERDATA, &g);
/* we do not call any curl_multi_socket*() function yet as we have no handles
added! */
event_base_dispatch(g.evbase);
/* this, of course, does not get called since the only way to stop this
program is via ctrl-C, but it is here to show how cleanup /would/ be
done. */
clean_fifo(&g);
event_del(&g.timer_event);
event_base_free(g.evbase);
curl_multi_cleanup(g.multi);
curl_global_cleanup();
return 0;
}
+122
View File
@@ -0,0 +1,122 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Preload domains to HSTS
* </DESC>
*/
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
struct entry {
const char *name;
const char *exp;
};
static const struct entry preload_hosts[] = {
{ "example.com", "20370320 01:02:03" },
{ "curl.se", "20370320 03:02:01" },
{ NULL, NULL } /* end of list marker */
};
struct state {
int index;
};
/* "read" is from the point of the library, it wants data from us. One domain
entry per invoke. */
static CURLSTScode hstsread(CURL *curl, struct curl_hstsentry *e,
void *userp)
{
const char *host;
const char *expire;
struct state *s = (struct state *)userp;
(void)curl;
host = preload_hosts[s->index].name;
expire = preload_hosts[s->index++].exp;
if(host && (strlen(host) < e->namelen)) {
strcpy(e->name, host);
e->includeSubDomains = 0;
strcpy(e->expire, expire);
fprintf(stderr, "HSTS preload '%s' until '%s'\n", host, expire);
}
else
return CURLSTS_DONE;
return CURLSTS_OK;
}
static CURLSTScode hstswrite(CURL *curl, struct curl_hstsentry *e,
struct curl_index *i, void *userp)
{
(void)curl;
(void)userp; /* we have no custom input */
printf("[%u/%u] %s %s\n", (unsigned int)i->index, (unsigned int)i->total,
e->name, e->expire);
return CURLSTS_OK;
}
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
struct state st = {0};
/* enable HSTS for this handle */
curl_easy_setopt(curl, CURLOPT_HSTS_CTRL, CURLHSTS_ENABLE);
/* function to call at first to populate the cache before the transfer */
curl_easy_setopt(curl, CURLOPT_HSTSREADFUNCTION, hstsread);
curl_easy_setopt(curl, CURLOPT_HSTSREADDATA, &st);
/* function to call after transfer to store the new state of the HSTS
cache */
curl_easy_setopt(curl, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
curl_easy_setopt(curl, CURLOPT_HSTSWRITEDATA, NULL);
/* use the domain with HTTP but due to the preload, it should do the
transfer using HTTPS */
curl_easy_setopt(curl, CURLOPT_URL, "http://curl.se");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+138
View File
@@ -0,0 +1,138 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Download a document and use libtidy to parse the HTML.
* </DESC>
*/
/*
* LibTidy => https://www.html-tidy.org/
*/
#include <stdio.h>
#include <tidy/tidy.h>
#include <tidy/tidybuffio.h>
#include <curl/curl.h>
/* curl write callback, to fill tidy's input buffer... */
uint write_cb(char *in, uint size, uint nmemb, TidyBuffer *out)
{
uint r;
r = size * nmemb;
tidyBufAppend(out, in, r);
return r;
}
/* Traverse the document tree */
void dumpNode(TidyDoc doc, TidyNode tnod, int indent)
{
TidyNode child;
for(child = tidyGetChild(tnod); child; child = tidyGetNext(child) ) {
ctmbstr name = tidyNodeGetName(child);
if(name) {
/* if it has a name, then it's an HTML tag ... */
TidyAttr attr;
printf("%*.*s%s ", indent, indent, "<", name);
/* walk the attribute list */
for(attr = tidyAttrFirst(child); attr; attr = tidyAttrNext(attr) ) {
printf("%s", tidyAttrName(attr));
tidyAttrValue(attr) ? printf("=\"%s\" ",
tidyAttrValue(attr)) : printf(" ");
}
printf(">\n");
}
else {
/* if it does not have a name, then it's probably text, cdata, etc... */
TidyBuffer buf;
tidyBufInit(&buf);
tidyNodeGetText(doc, child, &buf);
printf("%*.*s\n", indent, indent, buf.bp ? (char *)buf.bp : "");
tidyBufFree(&buf);
}
dumpNode(doc, child, indent + 4); /* recursive */
}
}
int main(int argc, char **argv)
{
CURL *curl;
char curl_errbuf[CURL_ERROR_SIZE];
TidyDoc tdoc;
TidyBuffer docbuf = {0};
TidyBuffer tidy_errbuf = {0};
CURLcode res;
if(argc != 2) {
printf("usage: %s <url>\n", argv[0]);
return 1;
}
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
tdoc = tidyCreate();
tidyOptSetBool(tdoc, TidyForceOutput, yes); /* try harder */
tidyOptSetInt(tdoc, TidyWrapLen, 4096);
tidySetErrorBuffer(tdoc, &tidy_errbuf);
tidyBufInit(&docbuf);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &docbuf);
res = curl_easy_perform(curl);
if(!res) {
res = tidyParseBuffer(tdoc, &docbuf); /* parse the input */
if(res >= 0) {
res = tidyCleanAndRepair(tdoc); /* fix any problems */
if(res >= 0) {
res = tidyRunDiagnostics(tdoc); /* load tidy error buffer */
if(res >= 0) {
dumpNode(tdoc, tidyGetRoot(tdoc), 0); /* walk the tree */
fprintf(stderr, "%s\n", tidy_errbuf.bp); /* show errors */
}
}
}
}
else
fprintf(stderr, "%s\n", curl_errbuf);
/* clean-up */
curl_easy_cleanup(curl);
}
tidyBufFree(&docbuf);
tidyBufFree(&tidy_errbuf);
tidyRelease(tdoc);
curl_global_cleanup();
return (int)res;
}
+305
View File
@@ -0,0 +1,305 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Get a web page, extract the title with libxml.
* </DESC>
Written by Lars Nilsson
GNU C++ compile command line suggestion (edit paths accordingly):
g++ -Wall -I/opt/curl/include -I/opt/libxml/include/libxml2 htmltitle.cpp \
-o htmltitle -L/opt/curl/lib -L/opt/libxml/lib -lcurl -lxml2
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <curl/curl.h>
#include <libxml/HTMLparser.h>
//
// Case-insensitive string comparison
//
#ifdef _WIN32
#define COMPARE(a, b) (!_stricmp((a), (b)))
#else
#define COMPARE(a, b) (!strcasecmp((a), (b)))
#endif
//
// libxml callback context structure
//
struct Context
{
Context(): addTitle(false) { }
bool addTitle;
std::string title;
};
//
// libcurl variables for error strings and returned data
static char errorBuffer[CURL_ERROR_SIZE];
static std::string buffer;
//
// libcurl write callback function
//
static size_t writer(char *data, size_t size, size_t nmemb,
std::string *writerData)
{
if(writerData == NULL)
return 0;
writerData->append(data, size*nmemb);
return size * nmemb;
}
//
// libcurl connection initialization
//
static bool init(CURL *&curl, const char *url)
{
CURLcode res;
curl = curl_easy_init();
if(!curl) {
fprintf(stderr, "Failed to create CURL handle\n");
return false;
}
res = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
if(res != CURLE_OK) {
fprintf(stderr, "Failed to set error buffer [%d]\n", res);
return false;
}
res = curl_easy_setopt(curl, CURLOPT_URL, url);
if(res != CURLE_OK) {
fprintf(stderr, "Failed to set URL [%s]\n", errorBuffer);
return false;
}
res = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
if(res != CURLE_OK) {
fprintf(stderr, "Failed to set redirect option [%s]\n", errorBuffer);
return false;
}
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
if(res != CURLE_OK) {
fprintf(stderr, "Failed to set writer [%s]\n", errorBuffer);
return false;
}
res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
if(res != CURLE_OK) {
fprintf(stderr, "Failed to set write data [%s]\n", errorBuffer);
return false;
}
return true;
}
//
// libxml start element callback function
//
static void StartElement(void *voidContext,
const xmlChar *name,
const xmlChar **attributes)
{
Context *context = static_cast<Context *>(voidContext);
if(COMPARE(reinterpret_cast<const char *>(name), "TITLE")) {
context->title = "";
context->addTitle = true;
}
(void)attributes;
}
//
// libxml end element callback function
//
static void EndElement(void *voidContext,
const xmlChar *name)
{
Context *context = static_cast<Context *>(voidContext);
if(COMPARE(reinterpret_cast<const char *>(name), "TITLE"))
context->addTitle = false;
}
//
// Text handling helper function
//
static void handleCharacters(Context *context,
const xmlChar *chars,
int length)
{
if(context->addTitle)
context->title.append(reinterpret_cast<const char *>(chars),
(unsigned long)length);
}
//
// libxml PCDATA callback function
//
static void Characters(void *voidContext,
const xmlChar *chars,
int length)
{
Context *context = static_cast<Context *>(voidContext);
handleCharacters(context, chars, length);
}
//
// libxml CDATA callback function
//
static void cdata(void *voidContext,
const xmlChar *chars,
int length)
{
Context *context = static_cast<Context *>(voidContext);
handleCharacters(context, chars, length);
}
//
// libxml SAX callback structure
//
static htmlSAXHandler saxHandler =
{
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
StartElement,
EndElement,
NULL,
Characters,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
cdata,
NULL,
0,
0,
0,
0,
NULL
};
//
// Parse given (assumed to be) HTML text and return the title
//
static void parseHtml(const std::string &html,
std::string &title)
{
htmlParserCtxtPtr ctxt;
Context context;
ctxt = htmlCreatePushParserCtxt(&saxHandler, &context, "", 0, "",
XML_CHAR_ENCODING_NONE);
htmlParseChunk(ctxt, html.c_str(), (int)html.size(), 0);
htmlParseChunk(ctxt, "", 0, 1);
htmlFreeParserCtxt(ctxt);
title = context.title;
}
int main(int argc, char *argv[])
{
CURL *curl = NULL;
CURLcode res;
std::string title;
// Ensure one argument is given
if(argc != 2) {
fprintf(stderr, "Usage: %s <url>\n", argv[0]);
return EXIT_FAILURE;
}
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
// Initialize CURL handle
if(!init(curl, argv[1])) {
fprintf(stderr, "Handle initialization failed\n");
curl_global_cleanup();
return EXIT_FAILURE;
}
// Retrieve content for the URL
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(res != CURLE_OK) {
fprintf(stderr, "Failed to get '%s' [%s]\n", argv[1], errorBuffer);
return EXIT_FAILURE;
}
// Parse the (assumed) HTML code
parseHtml(buffer, title);
// Display the extracted title
printf("Title: %s\n", title.c_str());
return (int)res;
}
+63
View File
@@ -0,0 +1,63 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Issue an HTTP 'OPTIONS *' request
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "OPTIONS");
/* issue an OPTIONS * request (no leading slash) */
curl_easy_setopt(curl, CURLOPT_REQUEST_TARGET, "*");
/* if this operation fails, allow risking a memory leak and do quick exit
from libcurl as this exits anyway */
curl_easy_setopt(curl, CURLOPT_QUICK_EXIT, 1L);
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
+63
View File
@@ -0,0 +1,63 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* simple HTTP POST using the easy interface
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
/* In Windows, this inits the Winsock stuff */
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be an https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, "http://postit.example.com/moo.cgi");
/* Now specify the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+263
View File
@@ -0,0 +1,263 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Multiplexed HTTP/2 downloads over a single connection
* </DESC>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef UNDER_CE
#define strerror(e) "?"
#else
#include <errno.h>
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
/* curl stuff */
#include <curl/curl.h>
#ifndef CURLPIPE_MULTIPLEX
/* This little trick makes sure that we do not enable pipelining for libcurls
old enough to not have this symbol. It is _not_ defined to zero in a recent
libcurl header. */
#define CURLPIPE_MULTIPLEX 0L
#endif
struct transfer {
FILE *out;
CURL *curl;
int num;
};
static void dump(const char *text, int num, unsigned char *ptr,
size_t size, char nohex)
{
size_t i;
size_t c;
unsigned int width = 0x10;
if(nohex)
/* without the hex output, we can fit more on screen */
width = 0x40;
fprintf(stderr, "%d %s, %lu bytes (0x%lx)\n",
num, text, (unsigned long)size, (unsigned long)size);
for(i = 0; i < size; i += width) {
fprintf(stderr, "%4.4lx: ", (unsigned long)i);
if(!nohex) {
/* hex not disabled, show it */
for(c = 0; c < width; c++)
if(i + c < size)
fprintf(stderr, "%02x ", ptr[i + c]);
else
fputs(" ", stderr);
}
for(c = 0; (c < width) && (i + c < size); c++) {
/* check for 0D0A; if found, skip past and start a new line of output */
if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
ptr[i + c + 1] == 0x0A) {
i += (c + 2 - width);
break;
}
fprintf(stderr, "%c",
(ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
/* check again for 0D0A, to avoid an extra \n if it's at width */
if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
ptr[i + c + 2] == 0x0A) {
i += (c + 3 - width);
break;
}
}
fputc('\n', stderr); /* newline */
}
}
static int my_trace(CURL *curl, curl_infotype type,
char *data, size_t size, void *userp)
{
const char *text;
struct transfer *t = (struct transfer *)userp;
int num = t->num;
(void)curl;
switch(type) {
case CURLINFO_TEXT:
fprintf(stderr, "== [%d] Info: %s", num, data);
return 0;
case CURLINFO_HEADER_OUT:
text = "=> Send header";
break;
case CURLINFO_DATA_OUT:
text = "=> Send data";
break;
case CURLINFO_SSL_DATA_OUT:
text = "=> Send SSL data";
break;
case CURLINFO_HEADER_IN:
text = "<= Recv header";
break;
case CURLINFO_DATA_IN:
text = "<= Recv data";
break;
case CURLINFO_SSL_DATA_IN:
text = "<= Recv SSL data";
break;
default: /* in case a new one is introduced to shock us */
return 0;
}
dump(text, num, (unsigned char *)data, size, 1);
return 0;
}
static int setup(struct transfer *t, int num)
{
char filename[128];
CURL *curl;
curl = t->curl = NULL;
t->num = num;
snprintf(filename, sizeof(filename), "dl-%d", num);
t->out = fopen(filename, "wb");
if(!t->out) {
fprintf(stderr, "error: could not open file %s for writing: %s\n",
filename, strerror(errno));
return 1;
}
curl = t->curl = curl_easy_init();
if(curl) {
/* write to this file */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, t->out);
/* set the same URL */
curl_easy_setopt(curl, CURLOPT_URL, "https://localhost:8443/index.html");
/* please be verbose */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
curl_easy_setopt(curl, CURLOPT_DEBUGDATA, t);
/* enlarge the receive buffer for potentially higher transfer speeds */
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 100000L);
/* HTTP/2 please */
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
#if (CURLPIPE_MULTIPLEX > 0)
/* wait for pipe connection to confirm */
curl_easy_setopt(curl, CURLOPT_PIPEWAIT, 1L);
#endif
}
return 0;
}
/*
* Download many transfers over HTTP/2, using the same connection!
*/
int main(int argc, char **argv)
{
CURLcode res;
struct transfer *trans;
CURLM *multi = NULL;
int i;
int still_running = 0; /* keep number of running handles */
int num_transfers;
if(argc > 1) {
/* if given a number, do that many transfers */
num_transfers = atoi(argv[1]);
if((num_transfers < 1) || (num_transfers > 1000))
num_transfers = 3; /* a suitable low default */
}
else
num_transfers = 3; /* a suitable low default */
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
trans = calloc(num_transfers, sizeof(*trans));
if(!trans) {
fprintf(stderr, "error allocating transfer structs\n");
goto error;
}
/* init a multi stack */
multi = curl_multi_init();
if(!multi)
goto error;
for(i = 0; i < num_transfers; i++) {
if(setup(&trans[i], i)) {
goto error;
}
/* add the individual transfer */
curl_multi_add_handle(multi, trans[i].curl);
}
curl_multi_setopt(multi, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
do {
CURLMcode mc = curl_multi_perform(multi, &still_running);
if(still_running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi, NULL, 0, 1000, NULL);
if(mc)
break;
} while(still_running);
error:
if(multi) {
for(i = 0; i < num_transfers; i++) {
curl_multi_remove_handle(multi, trans[i].curl);
curl_easy_cleanup(trans[i].curl);
if(trans[i].out)
fclose(trans[i].out);
}
curl_multi_cleanup(multi);
}
free(trans);
curl_global_cleanup();
return 0;
}
+188
View File
@@ -0,0 +1,188 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* HTTP/2 server push. Receive all data in memory.
* </DESC>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* curl stuff */
#include <curl/curl.h>
struct Memory {
char *memory;
size_t size;
};
static size_t write_cb(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct Memory *mem = (struct Memory *)userp;
char *ptr = realloc(mem->memory, mem->size + realsize + 1);
if(!ptr) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
#define MAX_FILES 10
static struct Memory files[MAX_FILES];
static int pushindex = 1;
static void init_memory(struct Memory *chunk)
{
chunk->memory = malloc(1); /* grown as needed with realloc */
chunk->size = 0; /* no data at this point */
}
static void setup(CURL *curl)
{
/* set the same URL */
curl_easy_setopt(curl, CURLOPT_URL, "https://localhost:8443/index.html");
/* HTTP/2 please */
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
/* we use a self-signed test server, skip verification during debugging */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
/* write data to a struct */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
init_memory(&files[0]);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &files[0]);
/* wait for pipe connection to confirm */
curl_easy_setopt(curl, CURLOPT_PIPEWAIT, 1L);
}
/* called when there is an incoming push */
static int server_push_callback(CURL *parent,
CURL *curl,
size_t num_headers,
struct curl_pushheaders *headers,
void *userp)
{
char *headp;
int *transfers = (int *)userp;
(void)parent;
(void)num_headers;
if(pushindex == MAX_FILES)
/* cannot fit anymore */
return CURL_PUSH_DENY;
/* write to this buffer */
init_memory(&files[pushindex]);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &files[pushindex]);
pushindex++;
headp = curl_pushheader_byname(headers, ":path");
if(headp)
fprintf(stderr, "* Pushed :path '%s'\n", headp /* skip :path + colon */);
(*transfers)++; /* one more */
return CURL_PUSH_OK;
}
/*
* Download a file over HTTP/2, take care of server push.
*/
int main(void)
{
CURL *curl;
CURLM *multi;
int transfers = 1; /* we start with one */
int i;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
/* init a multi stack */
multi = curl_multi_init();
curl = curl_easy_init();
/* set options */
setup(curl);
/* add the easy transfer */
curl_multi_add_handle(multi, curl);
curl_multi_setopt(multi, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
curl_multi_setopt(multi, CURLMOPT_PUSHFUNCTION, server_push_callback);
curl_multi_setopt(multi, CURLMOPT_PUSHDATA, &transfers);
while(transfers) {
struct CURLMsg *m;
int still_running; /* keep number of running handles */
int rc;
CURLMcode mcode = curl_multi_perform(multi, &still_running);
if(mcode)
break;
mcode = curl_multi_wait(multi, NULL, 0, 1000, &rc);
if(mcode)
break;
/*
* When doing server push, libcurl itself created and added one or more
* easy handles but *we* need to clean them up when they are done.
*/
do {
int msgq = 0;
m = curl_multi_info_read(multi, &msgq);
if(m && (m->msg == CURLMSG_DONE)) {
curl = m->easy_handle;
transfers--;
curl_multi_remove_handle(multi, curl);
curl_easy_cleanup(curl);
}
} while(m);
}
curl_multi_cleanup(multi);
curl_global_cleanup();
/* 'pushindex' is now the number of received transfers */
for(i = 0; i < pushindex; i++) {
/* do something fun with the data, and then free it when done */
free(files[i].memory);
}
return 0;
}
+288
View File
@@ -0,0 +1,288 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* HTTP/2 server push
* </DESC>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* curl stuff */
#include <curl/curl.h>
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
#ifndef CURLPIPE_MULTIPLEX
#error "too old libcurl, cannot do HTTP/2 server push!"
#endif
static FILE *out_download;
static void dump(const char *text, unsigned char *ptr, size_t size, char nohex)
{
size_t i;
size_t c;
unsigned int width = 0x10;
if(nohex)
/* without the hex output, we can fit more on screen */
width = 0x40;
fprintf(stderr, "%s, %lu bytes (0x%lx)\n",
text, (unsigned long)size, (unsigned long)size);
for(i = 0; i < size; i += width) {
fprintf(stderr, "%4.4lx: ", (unsigned long)i);
if(!nohex) {
/* hex not disabled, show it */
for(c = 0; c < width; c++)
if(i + c < size)
fprintf(stderr, "%02x ", ptr[i + c]);
else
fputs(" ", stderr);
}
for(c = 0; (c < width) && (i + c < size); c++) {
/* check for 0D0A; if found, skip past and start a new line of output */
if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
ptr[i + c + 1] == 0x0A) {
i += (c + 2 - width);
break;
}
fprintf(stderr, "%c",
(ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
/* check again for 0D0A, to avoid an extra \n if it's at width */
if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
ptr[i + c + 2] == 0x0A) {
i += (c + 3 - width);
break;
}
}
fputc('\n', stderr); /* newline */
}
}
static int my_trace(CURL *curl, curl_infotype type,
char *data, size_t size, void *userp)
{
const char *text;
(void)curl;
(void)userp;
switch(type) {
case CURLINFO_TEXT:
fprintf(stderr, "== Info: %s", data);
return 0;
case CURLINFO_HEADER_OUT:
text = "=> Send header";
break;
case CURLINFO_DATA_OUT:
text = "=> Send data";
break;
case CURLINFO_SSL_DATA_OUT:
text = "=> Send SSL data";
break;
case CURLINFO_HEADER_IN:
text = "<= Recv header";
break;
case CURLINFO_DATA_IN:
text = "<= Recv data";
break;
case CURLINFO_SSL_DATA_IN:
text = "<= Recv SSL data";
break;
default: /* in case a new one is introduced to shock us */
return 0;
}
dump(text, (unsigned char *)data, size, 1);
return 0;
}
#define OUTPUTFILE "dl"
static int setup(CURL *curl, const char *url)
{
out_download = fopen(OUTPUTFILE, "wb");
if(!out_download)
return 1; /* failed */
/* set the same URL */
curl_easy_setopt(curl, CURLOPT_URL, url);
/* HTTP/2 please */
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
/* we use a self-signed test server, skip verification during debugging */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
/* write to this file */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, out_download);
/* please be verbose */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
#if CURLPIPE_MULTIPLEX > 0
/* wait for pipe connection to confirm */
curl_easy_setopt(curl, CURLOPT_PIPEWAIT, 1L);
#endif
return 0; /* all is good */
}
static FILE *out_push;
/* called when there is an incoming push */
static int server_push_callback(CURL *parent,
CURL *curl,
size_t num_headers,
struct curl_pushheaders *headers,
void *userp)
{
char *headp;
size_t i;
int *transfers = (int *)userp;
char filename[128];
static unsigned int count = 0;
(void)parent;
snprintf(filename, sizeof(filename), "push%u", count++);
/* here's a new stream, save it in a new file for each new push */
out_push = fopen(filename, "wb");
if(!out_push) {
/* if we cannot save it, deny it */
fprintf(stderr, "Failed to create output file for push\n");
return CURL_PUSH_DENY;
}
/* write to this file */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, out_push);
fprintf(stderr, "**** push callback approves stream %u, got %lu headers!\n",
count, (unsigned long)num_headers);
for(i = 0; i < num_headers; i++) {
headp = curl_pushheader_bynum(headers, i);
fprintf(stderr, "**** header %lu: %s\n", (unsigned long)i, headp);
}
headp = curl_pushheader_byname(headers, ":path");
if(headp) {
fprintf(stderr, "**** The PATH is %s\n", headp /* skip :path + colon */);
}
(*transfers)++; /* one more */
return CURL_PUSH_OK;
}
/*
* Download a file over HTTP/2, take care of server push.
*/
int main(int argc, char *argv[])
{
CURLcode res;
CURL *curl;
CURLM *multi;
int transfers = 1; /* we start with one */
const char *url = "https://localhost:8443/index.html";
if(argc == 2)
url = argv[1];
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
/* init a multi stack */
multi = curl_multi_init();
if(!multi)
goto error;
curl = curl_easy_init();
/* set options */
if(!curl || setup(curl, url)) {
fprintf(stderr, "failed\n");
goto error;
}
curl_multi_setopt(multi, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
curl_multi_setopt(multi, CURLMOPT_PUSHFUNCTION, server_push_callback);
curl_multi_setopt(multi, CURLMOPT_PUSHDATA, &transfers);
/* add the easy transfer */
curl_multi_add_handle(multi, curl);
do {
struct CURLMsg *m;
int still_running; /* keep number of running handles */
CURLMcode mc = curl_multi_perform(multi, &still_running);
if(still_running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi, NULL, 0, 1000, NULL);
if(mc)
break;
/*
* A little caution when doing server push is that libcurl itself has
* created and added one or more easy handles but we need to clean them up
* when we are done.
*/
do {
int msgq = 0;
m = curl_multi_info_read(multi, &msgq);
if(m && (m->msg == CURLMSG_DONE)) {
curl = m->easy_handle;
transfers--;
curl_multi_remove_handle(multi, curl);
curl_easy_cleanup(curl);
}
} while(m);
} while(transfers); /* as long as we have transfers going */
error:
if(multi)
curl_multi_cleanup(multi);
curl_global_cleanup();
fclose(out_download);
if(out_push)
fclose(out_push);
return 0;
}
+375
View File
@@ -0,0 +1,375 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Multiplexed HTTP/2 uploads over a single connection
* </DESC>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifdef UNDER_CE
#define strerror(e) "?"
#else
#include <errno.h>
#endif
/* somewhat Unix-specific */
#ifndef _MSC_VER
#include <sys/time.h>
#include <unistd.h>
#endif
#ifdef _WIN32
#undef stat
#define stat _stat
#undef fstat
#define fstat _fstat
#define fileno _fileno
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
/* curl stuff */
#include <curl/curl.h>
#ifndef CURLPIPE_MULTIPLEX
/* This little trick makes sure that we do not enable pipelining for libcurls
old enough to not have this symbol. It is _not_ defined to zero in a recent
libcurl header. */
#define CURLPIPE_MULTIPLEX 0L
#endif
#ifdef _MSC_VER
#define gettimeofday(a, b) my_gettimeofday((a), (b))
static int my_gettimeofday(struct timeval *tp, void *tzp)
{
(void)tzp;
if(tp) {
/* Offset between 1601-01-01 and 1970-01-01 in 100 nanosec units */
#define WIN32_FT_OFFSET (116444736000000000)
union {
CURL_TYPEOF_CURL_OFF_T ns100; /* time since 1 Jan 1601 in 100ns units */
FILETIME ft;
} _now;
GetSystemTimeAsFileTime(&_now.ft);
tp->tv_usec = (long)((_now.ns100 / 10) % 1000000);
tp->tv_sec = (long)((_now.ns100 - WIN32_FT_OFFSET) / 10000000);
}
return 0;
}
#endif
struct input {
FILE *in;
FILE *out;
size_t bytes_read; /* count up */
CURL *curl;
int num;
};
static void dump(const char *text, int num, unsigned char *ptr,
size_t size, char nohex)
{
size_t i;
size_t c;
unsigned int width = 0x10;
if(nohex)
/* without the hex output, we can fit more on screen */
width = 0x40;
fprintf(stderr, "%d %s, %lu bytes (0x%lx)\n",
num, text, (unsigned long)size, (unsigned long)size);
for(i = 0; i < size; i += width) {
fprintf(stderr, "%4.4lx: ", (unsigned long)i);
if(!nohex) {
/* hex not disabled, show it */
for(c = 0; c < width; c++)
if(i + c < size)
fprintf(stderr, "%02x ", ptr[i + c]);
else
fputs(" ", stderr);
}
for(c = 0; (c < width) && (i + c < size); c++) {
/* check for 0D0A; if found, skip past and start a new line of output */
if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
ptr[i + c + 1] == 0x0A) {
i += (c + 2 - width);
break;
}
fprintf(stderr, "%c",
(ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
/* check again for 0D0A, to avoid an extra \n if it's at width */
if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
ptr[i + c + 2] == 0x0A) {
i += (c + 3 - width);
break;
}
}
fputc('\n', stderr); /* newline */
}
}
static int my_trace(CURL *curl, curl_infotype type,
char *data, size_t size, void *userp)
{
char timebuf[60];
const char *text;
struct input *i = (struct input *)userp;
int num = i->num;
static time_t epoch_offset;
static int known_offset;
struct timeval tv;
time_t secs;
struct tm *now;
(void)curl;
gettimeofday(&tv, NULL);
if(!known_offset) {
epoch_offset = time(NULL) - tv.tv_sec;
known_offset = 1;
}
secs = epoch_offset + tv.tv_sec;
now = localtime(&secs); /* not thread safe but we do not care */
snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
switch(type) {
case CURLINFO_TEXT:
fprintf(stderr, "%s [%d] Info: %s", timebuf, num, data);
return 0;
case CURLINFO_HEADER_OUT:
text = "=> Send header";
break;
case CURLINFO_DATA_OUT:
text = "=> Send data";
break;
case CURLINFO_SSL_DATA_OUT:
text = "=> Send SSL data";
break;
case CURLINFO_HEADER_IN:
text = "<= Recv header";
break;
case CURLINFO_DATA_IN:
text = "<= Recv data";
break;
case CURLINFO_SSL_DATA_IN:
text = "<= Recv SSL data";
break;
default: /* in case a new one is introduced to shock us */
return 0;
}
dump(text, num, (unsigned char *)data, size, 1);
return 0;
}
static size_t read_cb(char *ptr, size_t size, size_t nmemb, void *userp)
{
struct input *i = userp;
size_t retcode = fread(ptr, size, nmemb, i->in);
i->bytes_read += retcode;
return retcode;
}
static int setup(struct input *t, int num, const char *upload)
{
char url[256];
char filename[128];
struct stat file_info;
curl_off_t uploadsize;
CURL *curl;
curl = t->curl = NULL;
t->num = num;
snprintf(filename, sizeof(filename), "dl-%d", num);
t->out = fopen(filename, "wb");
if(!t->out) {
fprintf(stderr, "error: could not open file %s for writing: %s\n",
upload, strerror(errno));
return 1;
}
snprintf(url, sizeof(url), "https://localhost:8443/upload-%d", num);
t->in = fopen(upload, "rb");
if(!t->in) {
fprintf(stderr, "error: could not open file %s for reading: %s\n",
upload, strerror(errno));
fclose(t->out);
t->out = NULL;
return 1;
}
#ifdef UNDER_CE
/* !checksrc! disable BANNEDFUNC 1 */
if(stat(upload, &file_info) != 0) {
#else
if(fstat(fileno(t->in), &file_info) != 0) {
#endif
fprintf(stderr, "error: could not stat file %s: %s\n",
upload, strerror(errno));
fclose(t->out);
t->out = NULL;
return 1;
}
uploadsize = file_info.st_size;
curl = t->curl = curl_easy_init();
if(curl) {
/* write to this file */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, t->out);
/* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb);
/* read from this file */
curl_easy_setopt(curl, CURLOPT_READDATA, t);
/* provide the size of the upload */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, uploadsize);
/* send in the URL to store the upload as */
curl_easy_setopt(curl, CURLOPT_URL, url);
/* upload please */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* please be verbose */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
curl_easy_setopt(curl, CURLOPT_DEBUGDATA, t);
/* HTTP/2 please */
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
/* we use a self-signed test server, skip verification during debugging */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#if (CURLPIPE_MULTIPLEX > 0)
/* wait for pipe connection to confirm */
curl_easy_setopt(curl, CURLOPT_PIPEWAIT, 1L);
#endif
}
return 0;
}
/*
* Upload all files over HTTP/2, using the same physical connection!
*/
int main(int argc, char **argv)
{
CURLcode res;
struct input *trans;
CURLM *multi = NULL;
int i;
const char *filename = "index.html";
int still_running = 0; /* keep number of running handles */
int num_transfers;
if(argc > 1) {
/* if given a number, do that many transfers */
num_transfers = atoi(argv[1]);
if((num_transfers < 1) || (num_transfers > 1000))
num_transfers = 3; /* a suitable low default */
if(argc > 2)
/* if given a file name, upload this! */
filename = argv[2];
}
else
num_transfers = 3; /* a suitable low default */
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
trans = calloc(num_transfers, sizeof(*trans));
if(!trans) {
fprintf(stderr, "error allocating transfer structs\n");
goto error;
}
/* init a multi stack */
multi = curl_multi_init();
if(!multi)
goto error;
for(i = 0; i < num_transfers; i++) {
if(setup(&trans[i], i, filename)) {
goto error;
}
/* add the individual transfer */
curl_multi_add_handle(multi, trans[i].curl);
}
curl_multi_setopt(multi, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
/* We do HTTP/2 so let's stick to one connection per host */
curl_multi_setopt(multi, CURLMOPT_MAX_HOST_CONNECTIONS, 1L);
do {
CURLMcode mc = curl_multi_perform(multi, &still_running);
if(still_running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi, NULL, 0, 1000, NULL);
if(mc)
break;
} while(still_running);
error:
if(multi) {
for(i = 0; i < num_transfers; i++) {
curl_multi_remove_handle(multi, trans[i].curl);
curl_easy_cleanup(trans[i].curl);
if(trans[i].in)
fclose(trans[i].in);
if(trans[i].out)
fclose(trans[i].out);
}
curl_multi_cleanup(multi);
}
free(trans);
curl_global_cleanup();
return 0;
}
+51
View File
@@ -0,0 +1,51 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Checks if HTTP/3 support is present in libcurl.
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
curl_version_info_data *ver;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
ver = curl_version_info(CURLVERSION_NOW);
if(ver->features & CURL_VERSION_HTTP2)
printf("HTTP/2 support is present\n");
if(ver->features & CURL_VERSION_HTTP3)
printf("HTTP/3 support is present\n");
if(ver->features & CURL_VERSION_ALTSVC)
printf("Alt-svc support is present\n");
curl_global_cleanup();
return 0;
}
+58
View File
@@ -0,0 +1,58 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Very simple HTTP/3 GET
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
/* Use HTTP/3 but fallback to earlier HTTP if necessary */
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3);
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
+76
View File
@@ -0,0 +1,76 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* HTTP request with custom modified, removed and added headers
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
struct curl_slist *chunk = NULL;
/* Remove a header curl would otherwise add by itself */
chunk = curl_slist_append(chunk, "Accept:");
/* Add a custom header */
chunk = curl_slist_append(chunk, "Another: yes");
/* Modify a header curl otherwise adds differently */
chunk = curl_slist_append(chunk, "Host: example.com");
/* Add a header with "blank" contents to the right of the colon. Note that
we are then using a semicolon in the string we pass to curl! */
chunk = curl_slist_append(chunk, "X-silly-header;");
/* set our custom set of headers */
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl, CURLOPT_URL, "localhost");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
/* free the custom headers */
curl_slist_free_all(chunk);
}
curl_global_cleanup();
return (int)res;
}
+106
View File
@@ -0,0 +1,106 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* HTTP PUT using CURLOPT_POSTFIELDS
* </DESC>
*/
#include <stdio.h>
#include <fcntl.h>
#include <curl/curl.h>
static const char olivertwist[]=
"Among other public buildings in a certain town, which for many reasons "
"it will be prudent to refrain from mentioning, and to which I will assign "
"no fictitious name, there is one anciently common to most towns, great or "
"small: to ___, a workhouse; and in this workhouse was born; on a day and "
"date which I need not trouble myself to repeat, inasmuch as it can be of "
"no possible consequence to the reader, in this stage of the business at "
"all events; the item of mortality whose name is prefixed";
/* ... to the head of this chapter. String cut off to stick within the C90
509 byte limit. */
/*
* This example shows an HTTP PUT operation that sends a fixed buffer with
* CURLOPT_POSTFIELDS to the URL given as an argument.
*/
int main(int argc, char **argv)
{
CURL *curl;
CURLcode res;
char *url;
if(argc < 2)
return 1;
url = argv[1];
/* In Windows, this inits the Winsock stuff */
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
struct curl_slist *headers = NULL;
/* default type with postfields is application/x-www-form-urlencoded,
change it if you want */
headers = curl_slist_append(headers, "Content-Type: literature/classic");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
/* pass on content in request body. When CURLOPT_POSTFIELDSIZE is not used,
curl does strlen to get the size. */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, olivertwist);
/* override the POST implied by CURLOPT_POSTFIELDS
*
* Warning: CURLOPT_CUSTOMREQUEST is problematic, especially if you want
* to follow redirects. Be aware.
*/
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
/* specify target URL, and note that this URL should include a file
name, not only a directory */
curl_easy_setopt(curl, CURLOPT_URL, url);
/* Now run off and do what you have been told! */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
/* free headers */
curl_slist_free_all(headers);
}
curl_global_cleanup();
return (int)res;
}
+145
View File
@@ -0,0 +1,145 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* HTTP PUT with easy interface and read callback
* </DESC>
*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <curl/curl.h>
#ifdef _WIN32
#undef stat
#define stat _stat
#undef fstat
#define fstat _fstat
#define fileno _fileno
#endif
/*
* This example shows an HTTP PUT operation. PUTs a file given as a command
* line argument to the URL also given on the command line.
*
* This example also uses its own read callback.
*
* Here's an article on how to setup a PUT handler for Apache:
* http://www.apacheweek.com/features/put
*/
static size_t read_cb(char *ptr, size_t size, size_t nmemb, void *stream)
{
size_t retcode;
unsigned long nread;
/* in real-world cases, this would probably get this data differently
as this fread() stuff is exactly what the library already would do
by default internally */
retcode = fread(ptr, size, nmemb, stream);
if(retcode > 0) {
nread = (unsigned long)retcode;
fprintf(stderr, "*** We read %lu bytes from file\n", nread);
}
return retcode;
}
int main(int argc, char **argv)
{
CURL *curl;
CURLcode res;
FILE * hd_src;
struct stat file_info;
char *file;
char *url;
if(argc < 3)
return 1;
file = argv[1];
url = argv[2];
/* get a FILE * of the same file, could also be made with
fdopen() from the previous descriptor, but hey this is just
an example! */
hd_src = fopen(file, "rb");
if(!hd_src)
return 2;
/* get the file size of the local file */
#ifdef UNDER_CE
/* !checksrc! disable BANNEDFUNC 1 */
if(stat(file, &file_info) != 0) {
#else
if(fstat(fileno(hd_src), &file_info) != 0) {
#endif
fclose(hd_src);
return 1; /* cannot continue */
}
/* In Windows, this inits the Winsock stuff */
res = curl_global_init(CURL_GLOBAL_ALL);
if(res) {
fclose(hd_src);
return (int)res;
}
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
/* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb);
/* enable uploading (implies PUT over HTTP) */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* specify target URL, and note that this URL should include a file
name, not only a directory */
curl_easy_setopt(curl, CURLOPT_URL, url);
/* now specify which file to upload */
curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
/* provide the size of the upload, we typecast the value to curl_off_t
since we must be sure to use the correct data size */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)file_info.st_size);
/* Now run off and do what you have been told! */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(hd_src); /* close the local file */
curl_global_cleanup();
return (int)res;
}
+84
View File
@@ -0,0 +1,84 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Simple HTTPS GET
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
#ifdef SKIP_PEER_VERIFICATION
/*
* If you want to connect to a site who is not using a certificate that is
* signed by one of the certs in the CA bundle you have, you can skip the
* verification of the server's certificate. This makes the connection
* A LOT LESS SECURE.
*
* If you have a CA cert for the server stored someplace else than in the
* default bundle, then the CURLOPT_CAPATH option might come handy for
* you.
*/
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
#ifdef SKIP_HOSTNAME_VERIFICATION
/*
* If the site you are connecting to uses a different host name that what
* they have mentioned in their server certificate's commonName (or
* subjectAltName) fields, libcurl refuses to connect. You can skip this
* check, but it makes the connection insecure.
*/
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
/* cache the CA cert bundle in memory for a week */
curl_easy_setopt(curl, CURLOPT_CA_CACHE_TIMEOUT, 604800L);
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+135
View File
@@ -0,0 +1,135 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Send email with IMAP
* </DESC>
*/
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
/* This is a simple example showing how to send mail using libcurl's IMAP
* capabilities.
*
* Note that this example requires libcurl 7.30.0 or above.
*/
#define FROM "<sender@example.org>"
#define TO "<addressee@example.net>"
#define CC "<info@example.org>"
static const char *payload_text =
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
"To: " TO "\r\n"
"From: " FROM "(Example User)\r\n"
"Cc: " CC "(Another example User)\r\n"
"Message-ID: "
"<dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\r\n"
"Subject: IMAP example message\r\n"
"\r\n" /* empty line to divide headers from body, see RFC 5322 */
"The body of the message starts here.\r\n"
"\r\n"
"It could be a lot of lines, could be MIME encoded, whatever.\r\n"
"Check RFC 5322.\r\n";
struct upload_status {
size_t bytes_read;
};
static size_t read_cb(char *ptr, size_t size, size_t nmemb, void *userp)
{
struct upload_status *upload_ctx = (struct upload_status *)userp;
const char *data;
size_t room = size * nmemb;
if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
return 0;
}
data = &payload_text[upload_ctx->bytes_read];
if(*data) {
size_t len = strlen(data);
if(room < len)
len = room;
memcpy(ptr, data, len);
upload_ctx->bytes_read += len;
return len;
}
return 0;
}
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
size_t filesize;
long infilesize = LONG_MAX;
struct upload_status upload_ctx = { 0 };
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This creates a new message in folder "Sent". */
curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/Sent");
/* In this case, we are using a callback function to specify the data. You
* could just use the CURLOPT_READDATA option to specify a FILE pointer to
* read from. */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
filesize = strlen(payload_text);
if(filesize <= LONG_MAX)
infilesize = (long)filesize;
curl_easy_setopt(curl, CURLOPT_INFILESIZE, infilesize);
/* Perform the append */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+78
View File
@@ -0,0 +1,78 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Retrieve emails from a shared IMAP mailbox
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to fetch mail using libcurl's IMAP
* capabilities.
*
* Note that this example requires libcurl 7.66.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set the username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* Set the authorization identity (identity to act as) */
curl_easy_setopt(curl, CURLOPT_SASL_AUTHZID, "shared-mailbox");
/* Force PLAIN authentication */
curl_easy_setopt(curl, CURLOPT_LOGIN_OPTIONS, "AUTH=PLAIN");
/* This fetches message 1 from the user's inbox */
curl_easy_setopt(curl, CURLOPT_URL,
"imap://imap.example.com/INBOX/;UID=1");
/* Perform the fetch */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+78
View File
@@ -0,0 +1,78 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Copy an email from one IMAP folder to another
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to copy a mail from one mailbox folder
* to another using libcurl's IMAP capabilities.
*
* Note that this example requires libcurl 7.30.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This is source mailbox folder to select */
curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/INBOX");
/* Set the COPY command specifying the message ID and destination folder */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "COPY 1 FOLDER");
/* Note that to perform a move operation you need to perform the copy,
* then mark the original mail as Deleted and EXPUNGE or CLOSE. Please see
* imap-store.c for more information on deleting messages. */
/* Perform the custom request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+74
View File
@@ -0,0 +1,74 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Create a new IMAP folder
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to create a new mailbox folder using
* libcurl's IMAP capabilities.
*
* Note that this example requires libcurl 7.30.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This is just the server URL */
curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com");
/* Set the CREATE command specifying the new folder name */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "CREATE FOLDER");
/* Perform the custom request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+74
View File
@@ -0,0 +1,74 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Delete an IMAP folder
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to delete an existing mailbox folder
* using libcurl's IMAP capabilities.
*
* Note that this example requires libcurl 7.30.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This is just the server URL */
curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com");
/* Set the DELETE command specifying the existing folder */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE FOLDER");
/* Perform the custom request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+74
View File
@@ -0,0 +1,74 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Obtain information about an IMAP folder
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to obtain information about a mailbox
* folder using libcurl's IMAP capabilities via the EXAMINE command.
*
* Note that this example requires libcurl 7.30.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This is just the server URL */
curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com");
/* Set the EXAMINE command specifying the mailbox folder */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "EXAMINE OUTBOX");
/* Perform the custom request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+72
View File
@@ -0,0 +1,72 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Retrieve IMAP emails
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to fetch mail using libcurl's IMAP
* capabilities.
*
* Note that this example requires libcurl 7.30.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This fetches message 1 from the user's inbox */
curl_easy_setopt(curl, CURLOPT_URL,
"imap://imap.example.com/INBOX/;UID=1");
/* Perform the fetch */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+73
View File
@@ -0,0 +1,73 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* List the folders within an IMAP mailbox
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to list the folders within an IMAP
* mailbox.
*
* Note that this example requires libcurl 7.30.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This lists the folders within the user's mailbox. If you want to list
* the folders within a specific folder, for example the inbox, then
* specify the folder as a path in the URL such as /INBOX */
curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com");
/* Perform the list */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+75
View File
@@ -0,0 +1,75 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* List the subscribed IMAP folders
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to list the subscribed folders within
* an IMAP mailbox.
*
* Note that this example requires libcurl 7.30.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This is just the server URL */
curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com");
/* Set the LSUB command. Note the syntax is similar to that of a LIST
command. */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "LSUB \"\" *");
/* Perform the custom request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+87
View File
@@ -0,0 +1,87 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Get IMAP email with the multi interface
* </DESC>
*/
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
/* This is a simple example showing how to fetch mail using libcurl's IMAP
* capabilities. It builds on the imap-fetch.c example to demonstrate how to
* use libcurl's multi interface.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
CURLM *multi;
multi = curl_multi_init();
if(multi) {
int still_running = 1;
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This fetches message 1 from the user's inbox */
curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/"
"INBOX/;UID=1");
/* Tell the multi stack about our easy handle */
curl_multi_add_handle(multi, curl);
do {
CURLMcode mc = curl_multi_perform(multi, &still_running);
if(still_running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi, NULL, 0, 1000, NULL);
if(mc)
break;
} while(still_running);
/* Always cleanup */
curl_multi_remove_handle(multi, curl);
curl_multi_cleanup(multi);
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
+74
View File
@@ -0,0 +1,74 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Perform an IMAP noop
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to perform a noop using libcurl's IMAP
* capabilities.
*
* Note that this example requires libcurl 7.30.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This is just the server URL */
curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com");
/* Set the NOOP command */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "NOOP");
/* Perform the custom request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+78
View File
@@ -0,0 +1,78 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Search for new IMAP emails
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to search for new messages using
* libcurl's IMAP capabilities.
*
* Note that this example requires libcurl 7.30.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This is mailbox folder to select */
curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/INBOX");
/* Set the SEARCH command specifying what we want to search for. Note that
* this can contain a message sequence set and a number of search criteria
* keywords including flags such as ANSWERED, DELETED, DRAFT, FLAGGED, NEW,
* RECENT and SEEN. For more information about the search criteria please
* see RFC-3501 section 6.4.4. */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "SEARCH NEW");
/* Perform the custom request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+99
View File
@@ -0,0 +1,99 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* IMAP with implicit SSL
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to fetch mail using libcurl's IMAP
* capabilities. It builds on the imap-fetch.c example adding transport
* security to protect the authentication details from being snooped.
*
* Note that this example requires libcurl 7.30.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This fetches message 1 from the user's inbox. Note the use of
* imaps:// rather than imap:// to request an SSL based connection. */
curl_easy_setopt(curl, CURLOPT_URL,
"imaps://imap.example.com/INBOX/;UID=1");
/* If you want to connect to a site who is not using a certificate that is
* signed by one of the certs in the CA bundle you have, you can skip the
* verification of the server's certificate. This makes the connection
* A LOT LESS SECURE.
*
* If you have a CA cert for the server stored someplace else than in the
* default bundle, then the CURLOPT_CAPATH option might come handy for
* you. */
#ifdef SKIP_PEER_VERIFICATION
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
/* If the site you are connecting to uses a different host name that what
* they have mentioned in their server certificate's commonName (or
* subjectAltName) fields, libcurl refuses to connect. You can skip this
* check, but it makes the connection insecure. */
#ifdef SKIP_HOSTNAME_VERIFICATION
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
/* Since the traffic is encrypted, it is useful to turn on debug
* information within libcurl to see what is happening during the
* transfer */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* Perform the fetch */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+89
View File
@@ -0,0 +1,89 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Modify the properties of an email over IMAP
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to modify an existing mail using
* libcurl's IMAP capabilities with the STORE command.
*
* Note that this example requires libcurl 7.30.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This is the mailbox folder to select */
curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/INBOX");
/* Set the STORE command with the Deleted flag for message 1. Note that
* you can use the STORE command to set other flags such as Seen, Answered,
* Flagged, Draft and Recent. */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "STORE 1 +Flags \\Deleted");
/* Perform the custom request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
else {
/* Set the EXPUNGE command, although you can use the CLOSE command if you
* do not want to know the result of the STORE */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "EXPUNGE");
/* Perform the second custom request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+99
View File
@@ -0,0 +1,99 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* IMAP using TLS
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to fetch mail using libcurl's IMAP
* capabilities. It builds on the imap-fetch.c example adding transport
* security to protect the authentication details from being snooped.
*
* Note that this example requires libcurl 7.30.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This fetches message 1 from the user's inbox */
curl_easy_setopt(curl, CURLOPT_URL,
"imap://imap.example.com/INBOX/;UID=1");
/* In this example, we start with a plain text connection, and upgrade to
* Transport Layer Security (TLS) using the STARTTLS command. Be careful
* of using CURLUSESSL_TRY here, because if TLS upgrade fails, the
* transfer continues anyway - see the security discussion in the libcurl
* tutorial for more details. */
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
/* If your server does not have a valid certificate, then you can disable
* part of the Transport Layer Security protection by setting the
* CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false).
* curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
* curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
*
* That is, in general, a bad idea. It is still better than sending your
* authentication details in plain text though. Instead, you should get
* the issuer certificate (or the host certificate if the certificate is
* self-signed) and add it to the set of certificates that are known to
* libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See docs/SSLCERTS
* for more information. */
curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
/* Since the traffic is encrypted, it is useful to turn on debug
* information within libcurl to see what is happening during the
* transfer */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* Perform the fetch */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+57
View File
@@ -0,0 +1,57 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Use CURLOPT_INTERFACE to bind the outgoing socket to an interface
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* The interface needs to be a local existing interface over which you can
connect to the host in the URL. It can also specify an IP address, but
that address needs to be assigned one of the local network
interfaces. */
curl_easy_setopt(curl, CURLOPT_INTERFACE, "enp3s0");
curl_easy_setopt(curl, CURLOPT_URL, "https://curl.se/");
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+53
View File
@@ -0,0 +1,53 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* HTTPS GET using IPv6 only
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
curl_easy_setopt(curl, CURLOPT_URL, "https://curl.se/");
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+63
View File
@@ -0,0 +1,63 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Use the TCP keep-alive options
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* enable TCP keep-alive for this transfer */
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
/* keep-alive idle time to 120 seconds */
curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 120L);
/* interval time between keep-alive probes: 60 seconds */
curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 60L);
/* maximum number of keep-alive probes: 3 */
curl_easy_setopt(curl, CURLOPT_TCP_KEEPCNT, 3L);
curl_easy_setopt(curl, CURLOPT_URL, "https://curl.se/");
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+58
View File
@@ -0,0 +1,58 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Use CURLOPT_LOCALPORT to control local port number
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Try to use a local port number between 20000-20009 */
curl_easy_setopt(curl, CURLOPT_LOCALPORT, 20000L);
/* 10 means number of attempts, which starts with the number set in
CURLOPT_LOCALPORT. The lower value set, the smaller the chance it
works. */
curl_easy_setopt(curl, CURLOPT_LOCALPORTRANGE, 10L);
curl_easy_setopt(curl, CURLOPT_URL, "https://curl.se/");
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
@@ -0,0 +1,342 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Save failed transfer verbose log to disk
* </DESC>
*/
/*
*
* This example demonstrates per-transfer verbose logging to memory.
* The transfer's log is written to disk only if the transfer fails.
*
*/
#ifndef UNDER_CE
#include <errno.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <curl/curl.h>
#ifdef _WIN32
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <windows.h>
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#define unlink _unlink
#else
#include <strings.h>
#include <unistd.h>
#endif
struct mem {
/* 'buf' points to memory contents that is always zero terminated so that it
can be treated like a string if appropriate. 'recent' points to the most
recent data written to 'buf'. */
char *buf, *recent;
/* 'len' and 'allocsize' are the length and allocated size of 'buf' */
size_t len, allocsize;
};
struct transfer {
const char *url, *bodyfile, *logfile;
struct mem log;
FILE *bodyfp;
CURL *curl;
};
static void mem_reset(struct mem *mem)
{
free(mem->buf);
mem->buf = NULL;
mem->recent = NULL;
mem->len = 0;
mem->allocsize = 0;
}
/* expand free buffer space to needed size. return -1 or 'needed'. */
static int mem_need(struct mem *mem, size_t needed)
{
char *newbuf;
size_t newsize;
if(needed > (unsigned)INT_MAX)
return -1;
if(needed <= (mem->allocsize - mem->len))
return (int)needed;
/* min 4k makes reallocations much less frequent when lengths are small */
newsize = needed < 4096 ? 4096 : needed;
newsize += mem->len;
if(newsize < mem->len || newsize > (unsigned)INT_MAX)
return -1;
newbuf = realloc(mem->buf, newsize);
if(!newbuf)
return -1;
if(mem->recent && mem->buf != newbuf)
mem->recent = newbuf + (mem->recent - mem->buf);
mem->buf = newbuf;
mem->allocsize = newsize;
return (int)needed;
}
static int mem_addn(struct mem *mem, const char *buf, size_t len)
{
if(len + 1 < len || mem_need(mem, len + 1) < 0)
return -1;
mem->recent = mem->buf + mem->len;
memcpy(mem->recent, buf, len);
mem->len += len;
mem->buf[mem->len] = '\0';
return (int)len;
}
static int mem_add(struct mem *mem, const char *str)
{
return mem_addn(mem, str, strlen(str));
}
#if defined(__GNUC__) || defined(__clang__)
__attribute__((format(printf, 2, 3)))
#endif
static int mem_addf(struct mem *mem, const char *format, ...)
{
int i, x;
va_list va;
/* we need about 100 chars or less to write 95% of lines */
x = 128;
/* first try: there's probably enough memory to write everything.
second try: there's definitely enough memory to write everything. */
for(i = 0; i < 2; ++i) {
if(x < 0 || mem_need(mem, (size_t)x + 1) < 0)
break;
va_start(va, format);
x = vsnprintf(mem->buf + mem->len, mem->allocsize - mem->len, format, va);
va_end(va);
if(x >= 0 && (size_t)x < (mem->allocsize - mem->len)) {
mem->recent = mem->buf + mem->len;
mem->len += (size_t)x;
return x;
}
#if defined(_WIN32) && !defined(UNDER_CE)
/* Not all versions of Windows CRT vsnprintf are compliant with C99. Some
return -1 if buffer too small. Try _vscprintf to get the needed size. */
if(!i && x < 0) {
va_start(va, format);
x = _vscprintf(format, va);
va_end(va);
}
#endif
}
if(mem->buf)
mem->buf[mem->len] = '\0';
return -1;
}
static int mydebug(CURL *curl, curl_infotype type,
char *data, size_t size, void *userdata)
{
struct transfer *t = (struct transfer *)userdata;
static const char s_infotype[CURLINFO_END][3] = {
"* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
(void)curl;
switch(type) {
case CURLINFO_TEXT:
case CURLINFO_HEADER_OUT:
case CURLINFO_HEADER_IN:
/* mem_addn is faster than passing large data as %s to mem_addf */
mem_addn(&t->log, s_infotype[type], 2);
mem_addn(&t->log, data, size);
if(!size || data[size - 1] != '\n')
mem_add(&t->log, "\n");
break;
default:
break;
}
return 0;
}
static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
{
struct transfer *t = (struct transfer *)userdata;
return fwrite(ptr, size, nmemb, t->bodyfp);
}
int main(void)
{
CURLcode res;
unsigned i;
int total_failed = 0;
char errbuf[CURL_ERROR_SIZE] = { 0, };
struct transfer transfer[2];
memset(transfer, 0, sizeof(transfer));
transfer[0].url = "https://httpbin.org/get";
transfer[0].bodyfile = "200.txt";
transfer[0].logfile = "200_transfer_log.txt";
transfer[1].url = "https://httpbin.org/status/400";
transfer[1].bodyfile = "400.txt";
transfer[1].logfile = "400_transfer_log.txt";
res = curl_global_init(CURL_GLOBAL_ALL);
if(res) {
fprintf(stderr, "curl_global_init failed\n");
return (int)res;
}
/* You could enable global tracing for extra verbosity when verbosity is
enabled for a transfer. */
#if 0
curl_global_trace("all");
#endif
for(i = 0; i < sizeof(transfer)/sizeof(transfer[0]); ++i) {
int failed = 0;
struct transfer *t = &transfer[i];
t->curl = curl_easy_init();
if(!t->curl) {
fprintf(stderr, "curl_easy_init failed\n");
curl_global_cleanup();
return 1;
}
curl_easy_setopt(t->curl, CURLOPT_URL, t->url);
/* Enable following redirects */
curl_easy_setopt(t->curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Enable verbose logging to memory */
curl_easy_setopt(t->curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(t->curl, CURLOPT_DEBUGFUNCTION, mydebug);
curl_easy_setopt(t->curl, CURLOPT_DEBUGDATA, t);
/* Enable writing the body to a file */
curl_easy_setopt(t->curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(t->curl, CURLOPT_WRITEDATA, t);
/* Enable immediate error on HTTP status codes >= 400 in most cases,
instead of downloading the body to a file */
curl_easy_setopt(t->curl, CURLOPT_FAILONERROR, 1L);
/* Enable detailed error messages */
curl_easy_setopt(t->curl, CURLOPT_ERRORBUFFER, errbuf);
mem_addf(&t->log, "Downloading %s to file %s\n", t->url, t->bodyfile);
printf("%s", t->log.recent);
/* Create the body file */
t->bodyfp = fopen(t->bodyfile, "wb");
if(t->bodyfp) {
/* Perform the transfer */
CURLcode result = curl_easy_perform(t->curl);
/* Save the body file */
fclose(t->bodyfp);
t->bodyfp = NULL;
if(result == CURLE_OK) {
/* You could retrieve more information about the transfer here via
curl_easy_getinfo and mark the transfer as failed if needed. */
mem_addf(&t->log, "Transfer successful.\n");
fprintf(stderr, "%s", t->log.recent);
failed = 0;
}
else {
mem_addf(&t->log, "Transfer failed: (%d) %s\n", result,
(errbuf[0] ? errbuf : curl_easy_strerror(result)));
fprintf(stderr, "%s", t->log.recent);
failed = 1;
}
}
else {
#ifndef UNDER_CE
mem_addf(&t->log, "Failed to create body output file %s: %s\n",
t->bodyfile, strerror(errno));
fprintf(stderr, "%s", t->log.recent);
#endif
failed = 1;
}
if(failed) {
FILE *fp = fopen(t->logfile, "wb");
if(fp && t->log.len == fwrite(t->log.buf, 1, t->log.len, fp))
fprintf(stderr, "Transfer log written to %s\n", t->logfile);
#ifndef UNDER_CE
else {
fprintf(stderr, "Failed to write transfer log to %s: %s\n",
t->logfile, strerror(errno));
}
#endif
if(fp)
fclose(fp);
/* Depending on how the transfer failed a body file may or may not have
been written, and you may or may not want it. */
unlink(t->bodyfile);
++total_failed;
}
mem_reset(&t->log);
curl_easy_cleanup(t->curl);
t->curl = NULL;
printf("\n");
}
curl_global_cleanup();
return total_failed ? 1 : 0;
}
+72
View File
@@ -0,0 +1,72 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) James Fuller, <jim@webcomposite.com>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Set maximum number of persistent connections to 1.
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
const char *urls[] = { "https://example.com",
"https://curl.se",
"https://www.example/",
NULL /* end of list */
};
int i = 0;
/* Change the maximum number of persistent connection */
curl_easy_setopt(curl, CURLOPT_MAXCONNECTS, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* loop over the URLs */
while(urls[i]) {
curl_easy_setopt(curl, CURLOPT_URL, urls[i]);
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
i++;
}
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
+127
View File
@@ -0,0 +1,127 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* A basic application source code using the multi interface doing two
* transfers in parallel.
* </DESC>
*/
#include <stdio.h>
#include <string.h>
/* curl stuff */
#include <curl/curl.h>
/*
* Download an HTTP file and upload an FTP file simultaneously.
*/
#define HTTP_HANDLE 0 /* Index for the HTTP transfer */
#define FTP_HANDLE 1 /* Index for the FTP transfer */
#define HANDLECOUNT 2 /* Number of simultaneous transfers */
int main(void)
{
CURL *curl[HANDLECOUNT];
CURLM *multi;
int i;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
/* Allocate one curl handle per transfer */
for(i = 0; i < HANDLECOUNT; i++)
curl[i] = curl_easy_init();
/* set the options (I left out a few, you get the point anyway) */
curl_easy_setopt(curl[HTTP_HANDLE], CURLOPT_URL, "https://example.com");
curl_easy_setopt(curl[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
curl_easy_setopt(curl[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
/* init a multi stack */
multi = curl_multi_init();
if(multi) {
int still_running = 1; /* keep number of running handles */
CURLMsg *msg; /* for picking up messages with the transfer status */
int msgs_left; /* how many messages are left */
/* add the individual transfers */
for(i = 0; i < HANDLECOUNT; i++)
curl_multi_add_handle(multi, curl[i]);
while(still_running) {
CURLMcode mc = curl_multi_perform(multi, &still_running);
if(still_running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi, NULL, 0, 1000, NULL);
if(mc)
break;
}
/* See how the transfers went */
/* !checksrc! disable EQUALSNULL 1 */
while((msg = curl_multi_info_read(multi, &msgs_left)) != NULL) {
if(msg->msg == CURLMSG_DONE) {
int idx;
/* Find out which handle this message is about */
for(idx = 0; idx < HANDLECOUNT; idx++) {
int found = (msg->easy_handle == curl[idx]);
if(found)
break;
}
switch(idx) {
case HTTP_HANDLE:
printf("HTTP transfer completed with status %d\n", msg->data.result);
break;
case FTP_HANDLE:
printf("FTP transfer completed with status %d\n", msg->data.result);
break;
}
}
}
/* remove the transfers */
for(i = 0; i < HANDLECOUNT; i++)
curl_multi_remove_handle(multi, curl[i]);
curl_multi_cleanup(multi);
}
/* Free the curl handles */
for(i = 0; i < HANDLECOUNT; i++)
curl_easy_cleanup(curl[i]);
curl_global_cleanup();
return 0;
}
+171
View File
@@ -0,0 +1,171 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* multi interface and debug callback
* </DESC>
*/
#include <stdio.h>
#include <string.h>
/* curl stuff */
#include <curl/curl.h>
#define TRUE 1
static void dump(const char *text, FILE *stream, unsigned char *ptr,
size_t size, char nohex)
{
size_t i;
size_t c;
unsigned int width = 0x10;
if(nohex)
/* without the hex output, we can fit more on screen */
width = 0x40;
fprintf(stream, "%s, %10.10lu bytes (0x%8.8lx)\n",
text, (unsigned long)size, (unsigned long)size);
for(i = 0; i < size; i += width) {
fprintf(stream, "%4.4lx: ", (unsigned long)i);
if(!nohex) {
/* hex not disabled, show it */
for(c = 0; c < width; c++)
if(i + c < size)
fprintf(stream, "%02x ", ptr[i + c]);
else
fputs(" ", stream);
}
for(c = 0; (c < width) && (i + c < size); c++) {
/* check for 0D0A; if found, skip past and start a new line of output */
if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
ptr[i + c + 1] == 0x0A) {
i += (c + 2 - width);
break;
}
fprintf(stream, "%c",
(ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
/* check again for 0D0A, to avoid an extra \n if it's at width */
if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
ptr[i + c + 2] == 0x0A) {
i += (c + 3 - width);
break;
}
}
fputc('\n', stream); /* newline */
}
fflush(stream);
}
static int my_trace(CURL *curl, curl_infotype type,
unsigned char *data, size_t size,
void *userp)
{
const char *text;
(void)userp;
(void)curl;
switch(type) {
case CURLINFO_TEXT:
fprintf(stderr, "== Info: %s", data);
return 0;
case CURLINFO_HEADER_OUT:
text = "=> Send header";
break;
case CURLINFO_DATA_OUT:
text = "=> Send data";
break;
case CURLINFO_HEADER_IN:
text = "<= Recv header";
break;
case CURLINFO_DATA_IN:
text = "<= Recv data";
break;
default: /* in case a new one is introduced to shock us */
return 0;
}
dump(text, stderr, data, size, TRUE);
return 0;
}
/*
* Simply download an HTTP file.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
CURLM *multi;
/* set the options (I left out a few, you get the point anyway) */
curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* init a multi stack */
multi = curl_multi_init();
if(multi) {
int still_running = 0; /* keep number of running handles */
/* add the individual transfers */
curl_multi_add_handle(multi, curl);
do {
CURLMcode mc = curl_multi_perform(multi, &still_running);
if(still_running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi, NULL, 0, 1000, NULL);
if(mc)
break;
} while(still_running);
curl_multi_cleanup(multi);
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
+106
View File
@@ -0,0 +1,106 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* multi interface code doing two parallel HTTP transfers
* </DESC>
*/
#include <stdio.h>
#include <string.h>
/* curl stuff */
#include <curl/curl.h>
/*
* Simply download two HTTP files!
*/
int main(void)
{
CURL *curl;
CURL *curl2;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
curl2 = curl_easy_init();
if(curl && curl2) {
CURLM *multi;
/* set options */
curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
/* set options */
curl_easy_setopt(curl2, CURLOPT_URL, "http://localhost/");
/* init a multi stack */
multi = curl_multi_init();
if(multi) {
int still_running = 1; /* keep number of running handles */
/* add the individual transfers */
curl_multi_add_handle(multi, curl);
curl_multi_add_handle(multi, curl2);
while(still_running) {
CURLMsg *msg;
int queued;
CURLMcode mc = curl_multi_perform(multi, &still_running);
if(still_running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi, NULL, 0, 1000, NULL);
if(mc)
break;
do {
msg = curl_multi_info_read(multi, &queued);
if(msg) {
if(msg->msg == CURLMSG_DONE) {
/* a transfer ended */
fprintf(stderr, "Transfer completed\n");
}
}
} while(msg);
}
curl_multi_remove_handle(multi, curl);
curl_multi_remove_handle(multi, curl2);
curl_multi_cleanup(multi);
}
}
curl_easy_cleanup(curl);
curl_easy_cleanup(curl2);
curl_global_cleanup();
return 0;
}
+256
View File
@@ -0,0 +1,256 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* multi_socket API using libevent
* </DESC>
*/
#include <stdio.h>
#include <stdlib.h>
#include <event2/event.h>
#include <curl/curl.h>
static struct event_base *base;
static CURLM *multi;
static struct event *timeout;
struct curl_context {
struct event *event;
curl_socket_t sockfd;
};
static void curl_perform(int fd, short event, void *arg);
static struct curl_context *create_curl_context(curl_socket_t sockfd)
{
struct curl_context *context;
context = (struct curl_context *) malloc(sizeof(*context));
context->sockfd = sockfd;
context->event = event_new(base, sockfd, 0, curl_perform, context);
return context;
}
static void destroy_curl_context(struct curl_context *context)
{
event_del(context->event);
event_free(context->event);
free(context);
}
static void add_download(const char *url, int num)
{
char filename[50];
FILE *file;
CURL *curl;
snprintf(filename, sizeof(filename), "%d.download", num);
file = fopen(filename, "wb");
if(!file) {
fprintf(stderr, "Error opening %s\n", filename);
return;
}
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
curl_easy_setopt(curl, CURLOPT_PRIVATE, file);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_multi_add_handle(multi, curl);
fprintf(stderr, "Added download %s -> %s\n", url, filename);
}
static void check_multi_info(void)
{
char *done_url;
CURLMsg *message;
int pending;
CURL *curl;
FILE *file;
while((message = curl_multi_info_read(multi, &pending))) {
switch(message->msg) {
case CURLMSG_DONE:
/* Do not use message data after calling curl_multi_remove_handle() and
curl_easy_cleanup(). As per curl_multi_info_read() docs:
"WARNING: The data the returned pointer points to does not survive
calling curl_multi_cleanup, curl_multi_remove_handle or
curl_easy_cleanup." */
curl = message->easy_handle;
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &done_url);
curl_easy_getinfo(curl, CURLINFO_PRIVATE, &file);
printf("%s DONE\n", done_url);
curl_multi_remove_handle(multi, curl);
curl_easy_cleanup(curl);
if(file) {
fclose(file);
}
break;
default:
fprintf(stderr, "CURLMSG default\n");
break;
}
}
}
static void curl_perform(int fd, short event, void *arg)
{
int running_handles;
int flags = 0;
struct curl_context *context;
(void)fd;
if(event & EV_READ)
flags |= CURL_CSELECT_IN;
if(event & EV_WRITE)
flags |= CURL_CSELECT_OUT;
context = (struct curl_context *) arg;
curl_multi_socket_action(multi, context->sockfd, flags,
&running_handles);
check_multi_info();
}
static void on_timeout(evutil_socket_t fd, short events, void *arg)
{
int running_handles;
(void)fd;
(void)events;
(void)arg;
curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0,
&running_handles);
check_multi_info();
}
static int start_timeout(CURLM *multi, long timeout_ms, void *userp)
{
(void)multi;
(void)userp;
if(timeout_ms < 0) {
evtimer_del(timeout);
}
else {
struct timeval tv;
if(timeout_ms == 0)
timeout_ms = 1; /* 0 means call socket_action asap */
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
evtimer_del(timeout);
evtimer_add(timeout, &tv);
}
return 0;
}
static int handle_socket(CURL *curl, curl_socket_t s, int action, void *userp,
void *socketp)
{
struct curl_context *curl_context;
int events = 0;
(void)curl;
(void)userp;
switch(action) {
case CURL_POLL_IN:
case CURL_POLL_OUT:
case CURL_POLL_INOUT:
curl_context = socketp ?
(struct curl_context *) socketp : create_curl_context(s);
curl_multi_assign(multi, s, (void *) curl_context);
if(action != CURL_POLL_IN)
events |= EV_WRITE;
if(action != CURL_POLL_OUT)
events |= EV_READ;
events |= EV_PERSIST;
event_del(curl_context->event);
event_assign(curl_context->event, base, curl_context->sockfd,
(short)events, curl_perform, curl_context);
event_add(curl_context->event, NULL);
break;
case CURL_POLL_REMOVE:
if(socketp) {
event_del(((struct curl_context*) socketp)->event);
destroy_curl_context((struct curl_context*) socketp);
curl_multi_assign(multi, s, NULL);
}
break;
default:
abort();
}
return 0;
}
int main(int argc, char **argv)
{
CURLcode res;
if(argc <= 1)
return 0;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res) {
fprintf(stderr, "Could not init curl\n");
return (int)res;
}
base = event_base_new();
timeout = evtimer_new(base, on_timeout, NULL);
multi = curl_multi_init();
if(multi) {
curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, handle_socket);
curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, start_timeout);
while(argc-- > 1) {
add_download(argv[argc], argc);
}
event_base_dispatch(base);
curl_multi_cleanup(multi);
}
event_free(timeout);
event_base_free(base);
libevent_global_shutdown();
curl_global_cleanup();
return 0;
}
+131
View File
@@ -0,0 +1,131 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* using the multi interface to do a multipart formpost without blocking
* </DESC>
*/
/*
* Warning: this example uses the deprecated form api. See "multi-post.c"
* for a similar example using the mime api.
*/
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
struct curl_slist *headerlist = NULL;
static const char buf[] = "Expect:";
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
CURL_IGNORE_DEPRECATION(
/* Fill in the file upload field. This makes libcurl load data from
the given file name when curl_easy_perform() is called. */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "sendfile",
CURLFORM_FILE, "multi-formadd.c",
CURLFORM_END);
/* Fill in the filename field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "filename",
CURLFORM_COPYCONTENTS, "multi-formadd.c",
CURLFORM_END);
/* Fill in the submit field too, even if this is rarely needed */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "submit",
CURLFORM_COPYCONTENTS, "send",
CURLFORM_END);
)
/* initialize custom header list (stating that Expect: 100-continue is not
wanted */
headerlist = curl_slist_append(headerlist, buf);
curl = curl_easy_init();
if(curl) {
CURLM *multi;
multi = curl_multi_init();
if(multi) {
int still_running = 0;
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL,
"https://www.example.com/upload.cgi");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
CURL_IGNORE_DEPRECATION(
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
)
curl_multi_add_handle(multi, curl);
do {
CURLMcode mc = curl_multi_perform(multi, &still_running);
if(still_running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi, NULL, 0, 1000, NULL);
if(mc)
break;
} while(still_running);
curl_multi_cleanup(multi);
}
/* always cleanup */
curl_easy_cleanup(curl);
}
CURL_IGNORE_DEPRECATION(
/* then cleanup the formpost chain */
curl_formfree(formpost);
)
/* free slist */
curl_slist_free_all(headerlist);
curl_global_cleanup();
return 0;
}
+201
View File
@@ -0,0 +1,201 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* A basic application source code using the multi interface doing two
* transfers in parallel without curl_multi_wait/poll.
* </DESC>
*/
#include <stdio.h>
#include <string.h>
/* somewhat Unix-specific */
#ifndef _WIN32
#include <sys/time.h>
#include <unistd.h>
#endif
/* curl stuff */
#include <curl/curl.h>
/*
* Download an HTTP file and upload an FTP file simultaneously.
*/
#define HTTP_HANDLE 0 /* Index for the HTTP transfer */
#define FTP_HANDLE 1 /* Index for the FTP transfer */
#define HANDLECOUNT 2 /* Number of simultaneous transfers */
int main(void)
{
CURL *curl[HANDLECOUNT];
CURLM *multi;
int i;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
/* Allocate one curl handle per transfer */
for(i = 0; i < HANDLECOUNT; i++)
curl[i] = curl_easy_init();
/* set the options (I left out a few, you get the point anyway) */
curl_easy_setopt(curl[HTTP_HANDLE], CURLOPT_URL, "https://example.com");
curl_easy_setopt(curl[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
curl_easy_setopt(curl[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
/* init a multi stack */
multi = curl_multi_init();
if(multi) {
int still_running = 0; /* keep number of running handles */
CURLMsg *msg; /* for picking up messages with the transfer status */
int msgs_left; /* how many messages are left */
/* add the individual transfers */
for(i = 0; i < HANDLECOUNT; i++)
curl_multi_add_handle(multi, curl[i]);
/* we start some action by calling perform right away */
curl_multi_perform(multi, &still_running);
while(still_running) {
struct timeval timeout;
int rc; /* select() return code */
CURLMcode mc; /* curl_multi_fdset() return code */
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd = -1;
long curl_timeo = -1;
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
/* set a suitable timeout to play around with */
timeout.tv_sec = 1;
timeout.tv_usec = 0;
curl_multi_timeout(multi, &curl_timeo);
if(curl_timeo >= 0) {
#if defined(MSDOS) || defined(__AMIGA__)
timeout.tv_sec = (time_t)(curl_timeo / 1000);
#else
timeout.tv_sec = curl_timeo / 1000;
#endif
if(timeout.tv_sec > 1)
timeout.tv_sec = 1;
else
#if defined(MSDOS) || defined(__AMIGA__)
timeout.tv_usec = (time_t)(curl_timeo % 1000) * 1000;
#else
timeout.tv_usec = (int)(curl_timeo % 1000) * 1000;
#endif
}
/* get file descriptors from the transfers */
mc = curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
if(mc != CURLM_OK) {
fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
break;
}
/* On success the value of maxfd is guaranteed to be >= -1. We call
select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
to sleep 100ms, which is the minimum suggested value in the
curl_multi_fdset() doc. */
if(maxfd == -1) {
#ifdef _WIN32
Sleep(100);
rc = 0;
#else
/* Portable sleep for platforms other than Windows. */
struct timeval wait = {0};
wait.tv_usec = 100 * 1000; /* 100ms */
rc = select(0, NULL, NULL, NULL, &wait);
#endif
}
else {
/* Note that on some platforms 'timeout' may be modified by select().
If you need access to the original value save a copy beforehand. */
rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
}
switch(rc) {
case -1:
/* select error */
break;
case 0: /* timeout */
default: /* action */
curl_multi_perform(multi, &still_running);
break;
}
}
/* See how the transfers went */
/* !checksrc! disable EQUALSNULL 1 */
while((msg = curl_multi_info_read(multi, &msgs_left)) != NULL) {
if(msg->msg == CURLMSG_DONE) {
int idx;
/* Find out which handle this message is about */
for(idx = 0; idx < HANDLECOUNT; idx++) {
int found = (msg->easy_handle == curl[idx]);
if(found)
break;
}
switch(idx) {
case HTTP_HANDLE:
printf("HTTP transfer completed with status %d\n", msg->data.result);
break;
case FTP_HANDLE:
printf("FTP transfer completed with status %d\n", msg->data.result);
break;
}
}
}
curl_multi_cleanup(multi);
}
/* Free the curl handles */
for(i = 0; i < HANDLECOUNT; i++)
curl_easy_cleanup(curl[i]);
curl_global_cleanup();
return 0;
}
+114
View File
@@ -0,0 +1,114 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* using the multi interface to do a multipart formpost without blocking
* </DESC>
*/
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
int main(void)
{
curl_mime *form = NULL;
curl_mimepart *field = NULL;
struct curl_slist *headerlist = NULL;
static const char buf[] = "Expect:";
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
CURLM *multi;
multi = curl_multi_init();
if(multi) {
int still_running = 0;
/* Create the form */
form = curl_mime_init(curl);
/* Fill in the file upload field */
field = curl_mime_addpart(form);
curl_mime_name(field, "sendfile");
curl_mime_filedata(field, "multi-post.c");
/* Fill in the filename field */
field = curl_mime_addpart(form);
curl_mime_name(field, "filename");
curl_mime_data(field, "multi-post.c", CURL_ZERO_TERMINATED);
/* Fill in the submit field too, even if this is rarely needed */
field = curl_mime_addpart(form);
curl_mime_name(field, "submit");
curl_mime_data(field, "send", CURL_ZERO_TERMINATED);
/* initialize custom header list (stating that Expect: 100-continue is
not wanted */
headerlist = curl_slist_append(headerlist, buf);
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL,
"https://www.example.com/upload.cgi");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
curl_multi_add_handle(multi, curl);
do {
CURLMcode mc = curl_multi_perform(multi, &still_running);
if(still_running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi, NULL, 0, 1000, NULL);
if(mc)
break;
} while(still_running);
curl_multi_cleanup(multi);
}
/* always cleanup */
curl_easy_cleanup(curl);
}
/* then cleanup the form */
curl_mime_free(form);
/* free slist */
curl_slist_free_all(headerlist);
curl_global_cleanup();
return 0;
}
+87
View File
@@ -0,0 +1,87 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* using the multi interface to do a single download
* </DESC>
*/
#include <stdio.h>
#include <string.h>
/* curl stuff */
#include <curl/curl.h>
/*
* Simply download an HTTP file.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
CURLM *multi;
int still_running = 1; /* keep number of running handles */
/* set the options (I left out a few, you get the point anyway) */
curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
/* init a multi stack */
multi = curl_multi_init();
if(multi) {
/* add the individual transfers */
curl_multi_add_handle(multi, curl);
do {
CURLMcode mc = curl_multi_perform(multi, &still_running);
if(!mc)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi, NULL, 0, 1000, NULL);
if(mc) {
fprintf(stderr, "curl_multi_poll() failed, code %d.\n", (int)mc);
break;
}
} while(still_running);
curl_multi_remove_handle(multi, curl);
curl_multi_cleanup(multi);
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
+263
View File
@@ -0,0 +1,263 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* multi_socket API using libuv
* </DESC>
*/
/* Use the socket_action interface to download multiple files in parallel,
powered by libuv.
Requires libuv and (of course) libcurl.
See https://docs.libuv.org/en/v1.x/index.html libuv API documentation
*/
/* Requires: USE_LIBUV */
#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
#include <curl/curl.h>
/* object to pass to the callbacks */
struct datauv {
uv_timer_t timeout;
uv_loop_t *loop;
CURLM *multi;
};
struct curl_context {
uv_poll_t poll_handle;
curl_socket_t sockfd;
struct datauv *uv;
};
static struct curl_context *create_curl_context(curl_socket_t sockfd,
struct datauv *uv)
{
struct curl_context *context;
context = (struct curl_context *) malloc(sizeof(*context));
context->sockfd = sockfd;
context->uv = uv;
uv_poll_init_socket(uv->loop, &context->poll_handle, sockfd);
context->poll_handle.data = context;
return context;
}
static void curl_close_cb(uv_handle_t *handle)
{
struct curl_context *context = (struct curl_context *) handle->data;
free(context);
}
static void destroy_curl_context(struct curl_context *context)
{
uv_close((uv_handle_t *) &context->poll_handle, curl_close_cb);
}
static void add_download(const char *url, int num, CURLM *multi)
{
char filename[50];
FILE *file;
CURL *curl;
snprintf(filename, sizeof(filename), "%d.download", num);
file = fopen(filename, "wb");
if(!file) {
fprintf(stderr, "Error opening %s\n", filename);
return;
}
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
curl_easy_setopt(curl, CURLOPT_PRIVATE, file);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_multi_add_handle(multi, curl);
fprintf(stderr, "Added download %s -> %s\n", url, filename);
}
static void check_multi_info(struct curl_context *context)
{
char *done_url;
CURLMsg *message;
int pending;
CURL *curl;
FILE *file;
while((message = curl_multi_info_read(context->uv->multi, &pending))) {
switch(message->msg) {
case CURLMSG_DONE:
/* Do not use message data after calling curl_multi_remove_handle() and
curl_easy_cleanup(). As per curl_multi_info_read() docs:
"WARNING: The data the returned pointer points to does not survive
calling curl_multi_cleanup, curl_multi_remove_handle or
curl_easy_cleanup." */
curl = message->easy_handle;
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &done_url);
curl_easy_getinfo(curl, CURLINFO_PRIVATE, &file);
printf("%s DONE\n", done_url);
curl_multi_remove_handle(context->uv->multi, curl);
curl_easy_cleanup(curl);
if(file) {
fclose(file);
}
break;
default:
fprintf(stderr, "CURLMSG default\n");
break;
}
}
}
/* callback from libuv on socket activity */
static void on_uv_socket(uv_poll_t *req, int status, int events)
{
int running_handles;
int flags = 0;
struct curl_context *context = (struct curl_context *) req->data;
(void)status;
if(events & UV_READABLE)
flags |= CURL_CSELECT_IN;
if(events & UV_WRITABLE)
flags |= CURL_CSELECT_OUT;
curl_multi_socket_action(context->uv->multi, context->sockfd, flags,
&running_handles);
check_multi_info(context);
}
/* callback from libuv when timeout expires */
static void on_uv_timeout(uv_timer_t *req)
{
struct curl_context *context = (struct curl_context *) req->data;
if(context) {
int running_handles;
curl_multi_socket_action(context->uv->multi, CURL_SOCKET_TIMEOUT, 0,
&running_handles);
check_multi_info(context);
}
}
/* callback from libcurl to update the timeout expiry */
static int cb_timeout(CURLM *multi, long timeout_ms, void *userp)
{
struct datauv *uv = (struct datauv *)userp;
(void)multi;
if(timeout_ms < 0)
uv_timer_stop(&uv->timeout);
else {
if(timeout_ms == 0)
timeout_ms = 1; /* 0 means call curl_multi_socket_action asap but NOT
within the callback itself */
uv_timer_start(&uv->timeout, on_uv_timeout, (uint64_t)timeout_ms,
0); /* do not repeat */
}
return 0;
}
/* callback from libcurl to update socket activity to wait for */
static int cb_socket(CURL *curl, curl_socket_t s, int action,
void *userp, void *socketp)
{
struct datauv *uv = (struct datauv *)userp;
struct curl_context *curl_context;
int events = 0;
(void)curl;
switch(action) {
case CURL_POLL_IN:
case CURL_POLL_OUT:
case CURL_POLL_INOUT:
curl_context = socketp ?
(struct curl_context *) socketp : create_curl_context(s, uv);
curl_multi_assign(uv->multi, s, (void *) curl_context);
if(action != CURL_POLL_IN)
events |= UV_WRITABLE;
if(action != CURL_POLL_OUT)
events |= UV_READABLE;
uv_poll_start(&curl_context->poll_handle, events, on_uv_socket);
break;
case CURL_POLL_REMOVE:
if(socketp) {
uv_poll_stop(&((struct curl_context*)socketp)->poll_handle);
destroy_curl_context((struct curl_context*) socketp);
curl_multi_assign(uv->multi, s, NULL);
}
break;
default:
abort();
}
return 0;
}
int main(int argc, char **argv)
{
CURLcode res;
struct datauv uv = { 0 };
int running_handles;
if(argc <= 1)
return 0;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
uv.loop = uv_default_loop();
uv_timer_init(uv.loop, &uv.timeout);
uv.multi = curl_multi_init();
if(uv.multi) {
curl_multi_setopt(uv.multi, CURLMOPT_SOCKETFUNCTION, cb_socket);
curl_multi_setopt(uv.multi, CURLMOPT_SOCKETDATA, &uv);
curl_multi_setopt(uv.multi, CURLMOPT_TIMERFUNCTION, cb_timeout);
curl_multi_setopt(uv.multi, CURLMOPT_TIMERDATA, &uv);
while(argc-- > 1) {
add_download(argv[argc], argc, uv.multi);
}
/* kickstart the thing */
curl_multi_socket_action(uv.multi, CURL_SOCKET_TIMEOUT, 0,
&running_handles);
uv_run(uv.loop, UV_RUN_DEFAULT);
curl_multi_cleanup(uv.multi);
}
curl_global_cleanup();
return 0;
}
+104
View File
@@ -0,0 +1,104 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* A multi-threaded program using pthreads to fetch several files at once
* </DESC>
*/
/* Requires: HAVE_PTHREAD_H */
#include <stdio.h>
#include <pthread.h>
#include <curl/curl.h>
#define NUMT 4
/*
List of URLs to fetch.
If you intend to use an SSL-based protocol here you might need to setup TLS
library mutex callbacks as described here:
https://curl.se/libcurl/c/threadsafe.html
*/
static const char * const urls[NUMT]= {
"https://curl.se/",
"ftp://example.com/",
"https://example.net/",
"www.example"
};
static void *pull_one_url(void *pindex)
{
CURL *curl;
curl = curl_easy_init();
if(curl) {
int i = *(int *)pindex;
curl_easy_setopt(curl, CURLOPT_URL, urls[i]);
(void)curl_easy_perform(curl); /* ignores error */
curl_easy_cleanup(curl);
}
return NULL;
}
/*
int pthread_create(pthread_t *new_thread_ID,
const pthread_attr_t *attr,
void * (*start_func)(void *), void *arg);
*/
int main(void)
{
CURLcode res;
pthread_t tid[NUMT];
int i;
/* Must initialize libcurl before any threads are started */
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
for(i = 0; i < NUMT; i++) {
int error = pthread_create(&tid[i],
NULL, /* default attributes please */
pull_one_url,
(void *)&i);
if(error)
fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
else
fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
}
/* now wait for all threads to terminate */
for(i = 0; i < NUMT; i++) {
pthread_join(tid[i], NULL);
fprintf(stderr, "Thread %d terminated\n", i);
}
curl_global_cleanup();
return 0;
}
+54
View File
@@ -0,0 +1,54 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Use credentials from .netrc
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
curl_easy_setopt(curl, CURLOPT_NETRC_FILE,
"/home/daniel/s3cr3ts.txt");
curl_easy_setopt(curl, CURLOPT_URL, "https://curl.se/");
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+80
View File
@@ -0,0 +1,80 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Basic URL API use.
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
#if !CURL_AT_LEAST_VERSION(7, 62, 0)
#error "this example requires curl 7.62.0 or later"
#endif
int main(void)
{
CURLU *h;
CURLUcode uc;
char *host;
char *path;
h = curl_url(); /* get a handle to work with */
if(!h)
return 1;
/* parse a full URL */
uc = curl_url_set(h, CURLUPART_URL, "http://example.com/path/index.html", 0);
if(uc)
goto fail;
/* extract hostname from the parsed URL */
uc = curl_url_get(h, CURLUPART_HOST, &host, 0);
if(!uc) {
printf("Host name: %s\n", host);
curl_free(host);
}
/* extract the path from the parsed URL */
uc = curl_url_get(h, CURLUPART_PATH, &path, 0);
if(!uc) {
printf("Path: %s\n", path);
curl_free(path);
}
/* redirect with a relative URL */
uc = curl_url_set(h, CURLUPART_URL, "../another/second.html", 0);
if(uc)
goto fail;
/* extract the new, updated path */
uc = curl_url_get(h, CURLUPART_PATH, &path, 0);
if(!uc) {
printf("Path: %s\n", path);
curl_free(path);
}
fail:
curl_url_cleanup(h); /* free URL handle */
return 0;
}
+73
View File
@@ -0,0 +1,73 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* reusing handles to do HTTP persistent connections
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
/* get the first document */
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* get another document from the same server using the same
connection */
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/docs/");
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+77
View File
@@ -0,0 +1,77 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Retrieve emails from a shared POP3 mailbox
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to retrieve mail using libcurl's POP3
* capabilities.
*
* Note that this example requires libcurl 7.66.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set the username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* Set the authorization identity (identity to act as) */
curl_easy_setopt(curl, CURLOPT_SASL_AUTHZID, "shared-mailbox");
/* Force PLAIN authentication */
curl_easy_setopt(curl, CURLOPT_LOGIN_OPTIONS, "AUTH=PLAIN");
/* This retrieves message 1 from the user's mailbox */
curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com/1");
/* Perform the retr */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+77
View File
@@ -0,0 +1,77 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Delete POP3 emails
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to delete an existing mail using
* libcurl's POP3 capabilities.
*
* Note that this example requires libcurl 7.26.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* You can specify the message either in the URL or DELE command */
curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com/1");
/* Set the DELE command */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELE");
/* Do not perform a transfer as DELE returns no data */
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
/* Perform the custom request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+71
View File
@@ -0,0 +1,71 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* List the contents of a POP3 mailbox
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example using libcurl's POP3 capabilities to list the
* contents of a mailbox.
*
* Note that this example requires libcurl 7.20.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This lists every message of the given mailbox */
curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com");
/* Perform the list */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+88
View File
@@ -0,0 +1,88 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Get POP3 email using the multi interface
* </DESC>
*/
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
/* This is a simple example showing how to retrieve mail using libcurl's POP3
* capabilities. It builds on the pop3-retr.c example to demonstrate how to use
* libcurl's multi interface.
*/
int main(void)
{
CURLcode res;
CURL *curl;
res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
CURLM *multi;
multi = curl_multi_init();
if(multi) {
int still_running = 1;
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This retrieves message 1 from the user's mailbox */
curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com/1");
/* Tell the multi stack about our easy handle */
curl_multi_add_handle(multi, curl);
do {
CURLMcode mc = curl_multi_perform(multi, &still_running);
if(still_running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi, NULL, 0, 1000, NULL);
if(mc)
break;
} while(still_running);
/* Always cleanup */
curl_multi_remove_handle(multi, curl);
curl_multi_cleanup(multi);
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
+77
View File
@@ -0,0 +1,77 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Perform a POP3 noop
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to perform a noop using libcurl's POP3
* capabilities.
*
* Note that this example requires libcurl 7.26.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This is just the server URL */
curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com");
/* Set the NOOP command */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "NOOP");
/* Do not perform a transfer as NOOP returns no data */
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
/* Perform the custom request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+71
View File
@@ -0,0 +1,71 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Retrieve POP3 email
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to retrieve mail using libcurl's POP3
* capabilities.
*
* Note that this example requires libcurl 7.20.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This retrieves message 1 from the user's mailbox */
curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com/1");
/* Perform the retr */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+98
View File
@@ -0,0 +1,98 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Get POP3 email using implicit SSL
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to retrieve mail using libcurl's POP3
* capabilities. It builds on the pop3-retr.c example adding transport
* security to protect the authentication details from being snooped.
*
* Note that this example requires libcurl 7.20.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This retrieves message 1 from the user's mailbox. Note the use of *
pop3s:// rather than pop3:// to request an SSL based connection. */
curl_easy_setopt(curl, CURLOPT_URL, "pop3s://pop.example.com/1");
/* If you want to connect to a site who is not using a certificate that is
* signed by one of the certs in the CA bundle you have, you can skip the
* verification of the server's certificate. This makes the connection
* A LOT LESS SECURE.
*
* If you have a CA cert for the server stored someplace else than in the
* default bundle, then the CURLOPT_CAPATH option might come handy for
* you. */
#ifdef SKIP_PEER_VERIFICATION
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
/* If the site you are connecting to uses a different host name that what
* they have mentioned in their server certificate's commonName (or
* subjectAltName) fields, libcurl refuses to connect. You can skip this
* check, but it makes the connection insecure. */
#ifdef SKIP_HOSTNAME_VERIFICATION
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
/* Since the traffic is encrypted, it is useful to turn on debug
* information within libcurl to see what is happening during the
* transfer */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* Perform the retr */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+77
View File
@@ -0,0 +1,77 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Obtain POP3 message statistics
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to obtain message statistics using
* libcurl's POP3 capabilities.
*
* Note that this example requires libcurl 7.26.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This is just the server URL */
curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com");
/* Set the STAT command */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "STAT");
/* Do not perform a transfer as the data is in the response */
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
/* Perform the custom request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+98
View File
@@ -0,0 +1,98 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* POP3 using TLS
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to retrieve mail using libcurl's POP3
* capabilities. It builds on the pop3-retr.c example adding transport
* security to protect the authentication details from being snooped.
*
* Note that this example requires libcurl 7.20.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This retrieves message 1 from the user's mailbox */
curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com/1");
/* In this example, we start with a plain text connection, and upgrade to
* Transport Layer Security (TLS) using the STLS command. Be careful of
* using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer
* continues anyway - see the security discussion in the libcurl tutorial
* for more details. */
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
/* If your server does not have a valid certificate, then you can disable
* part of the Transport Layer Security protection by setting the
* CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false).
* curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
* curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
*
* That is, in general, a bad idea. It is still better than sending your
* authentication details in plain text though. Instead, you should get
* the issuer certificate (or the host certificate if the certificate is
* self-signed) and add it to the set of certificates that are known to
* libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See docs/SSLCERTS
* for more information. */
curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
/* Since the traffic is encrypted, it is useful to turn on debug
* information within libcurl to see what is happening during the
* transfer */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* Perform the retr */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+74
View File
@@ -0,0 +1,74 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* POP3 example showing how to retrieve only the headers of an email
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example showing how to retrieve only the headers of a mail
* using libcurl's POP3 capabilities.
*
* Note that this example requires libcurl 7.26.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This is just the server URL */
curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com");
/* Set the TOP command for message 1 to only include the headers */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "TOP 1 0");
/* Perform the custom request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}
+74
View File
@@ -0,0 +1,74 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* List the contents of a POP3 mailbox by unique ID
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* This is a simple example using libcurl's POP3 capabilities to list the
* contents of a mailbox by unique ID.
*
* Note that this example requires libcurl 7.26.0 or above.
*/
int main(void)
{
CURL *curl;
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res)
return (int)res;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This is just the server URL */
curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com");
/* Set the UIDL command */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "UIDL");
/* Perform the custom request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)res;
}

Some files were not shown because too many files have changed in this diff Show More