1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-22 08:08:50 -05:00

examples: make the C++ examples follow our code style too

At least mostly, not counting // comments.
This commit is contained in:
Daniel Stenberg 2017-01-08 16:38:54 +01:00
parent ed2fcd5428
commit a41e8592d6
2 changed files with 85 additions and 125 deletions

View File

@ -40,8 +40,8 @@
* Note: * Note:
* For the sake of simplicity, URL is hard coded to "www.google.com" * For the sake of simplicity, URL is hard coded to "www.google.com"
* *
* This is purely a demo app, all retrieved data is simply discarded by the write * This is purely a demo app, all retrieved data is simply discarded by the
* callback. * write callback.
*/ */
@ -85,14 +85,12 @@ static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
/* cancel running timer */ /* cancel running timer */
timer.cancel(); timer.cancel();
if(timeout_ms > 0) if(timeout_ms > 0) {
{
/* update timer */ /* update timer */
timer.expires_from_now(boost::posix_time::millisec(timeout_ms)); timer.expires_from_now(boost::posix_time::millisec(timeout_ms));
timer.async_wait(boost::bind(&timer_cb, _1, g)); timer.async_wait(boost::bind(&timer_cb, _1, g));
} }
else else {
{
/* call timeout function immediately */ /* call timeout function immediately */
boost::system::error_code error; /*success*/ boost::system::error_code error; /*success*/
timer_cb(error, g); timer_cb(error, g);
@ -104,11 +102,9 @@ static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
/* Die if we get a bad CURLMcode somewhere */ /* Die if we get a bad CURLMcode somewhere */
static void mcode_or_die(const char *where, CURLMcode code) static void mcode_or_die(const char *where, CURLMcode code)
{ {
if(CURLM_OK != code) if(CURLM_OK != code) {
{
const char *s; const char *s;
switch(code) switch(code) {
{
case CURLM_CALL_MULTI_PERFORM: case CURLM_CALL_MULTI_PERFORM:
s = "CURLM_CALL_MULTI_PERFORM"; s = "CURLM_CALL_MULTI_PERFORM";
break; break;
@ -158,10 +154,8 @@ static void check_multi_info(GlobalInfo *g)
fprintf(MSG_OUT, "\nREMAINING: %d", g->still_running); fprintf(MSG_OUT, "\nREMAINING: %d", g->still_running);
while((msg = curl_multi_info_read(g->multi, &msgs_left))) while((msg = curl_multi_info_read(g->multi, &msgs_left))) {
{ if(msg->msg == CURLMSG_DONE) {
if(msg->msg == CURLMSG_DONE)
{
easy = msg->easy_handle; easy = msg->easy_handle;
res = msg->data.result; res = msg->data.result;
curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn); curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn);
@ -177,13 +171,13 @@ static void check_multi_info(GlobalInfo *g)
/* Called by asio when there is an action on a socket */ /* Called by asio when there is an action on a socket */
static void event_cb(GlobalInfo *g, boost::asio::ip::tcp::socket *tcp_socket, static void event_cb(GlobalInfo *g, boost::asio::ip::tcp::socket *tcp_socket,
int action, const boost::system::error_code & error, int *fdp) int action, const boost::system::error_code & error,
int *fdp)
{ {
fprintf(MSG_OUT, "\nevent_cb: action=%d", action); fprintf(MSG_OUT, "\nevent_cb: action=%d", action);
/* make sure the event matches what are wanted */ /* make sure the event matches what are wanted */
if(*fdp == action || *fdp == CURL_POLL_INOUT) if(*fdp == action || *fdp == CURL_POLL_INOUT) {
{
curl_socket_t s = tcp_socket->native_handle(); curl_socket_t s = tcp_socket->native_handle();
CURLMcode rc; CURLMcode rc;
if(error) if(error)
@ -193,8 +187,7 @@ static void event_cb(GlobalInfo *g, boost::asio::ip::tcp::socket *tcp_socket,
mcode_or_die("event_cb: curl_multi_socket_action", rc); mcode_or_die("event_cb: curl_multi_socket_action", rc);
check_multi_info(g); check_multi_info(g);
if(g->still_running <= 0) if(g->still_running <= 0) {
{
fprintf(MSG_OUT, "\nlast transfer done, kill timeout"); fprintf(MSG_OUT, "\nlast transfer done, kill timeout");
timer.cancel(); timer.cancel();
} }
@ -203,17 +196,16 @@ static void event_cb(GlobalInfo *g, boost::asio::ip::tcp::socket *tcp_socket,
* the socket may have been closed and/or fdp may have been changed * the socket may have been closed and/or fdp may have been changed
* in curl_multi_socket_action(), so check them both */ * in curl_multi_socket_action(), so check them both */
if(!error && socket_map.find(s) != socket_map.end() && if(!error && socket_map.find(s) != socket_map.end() &&
(*fdp == action || *fdp == CURL_POLL_INOUT)) (*fdp == action || *fdp == CURL_POLL_INOUT)) {
{ if(action == CURL_POLL_IN) {
if(action == CURL_POLL_IN)
{
tcp_socket->async_read_some(boost::asio::null_buffers(), tcp_socket->async_read_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket, action, _1, fdp)); boost::bind(&event_cb, g, tcp_socket,
action, _1, fdp));
} }
if(action == CURL_POLL_OUT) if(action == CURL_POLL_OUT) {
{
tcp_socket->async_write_some(boost::asio::null_buffers(), tcp_socket->async_write_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket, action, _1, fdp)); boost::bind(&event_cb, g, tcp_socket,
action, _1, fdp));
} }
} }
} }
@ -222,12 +214,12 @@ static void event_cb(GlobalInfo *g, boost::asio::ip::tcp::socket *tcp_socket,
/* Called by asio when our timeout expires */ /* Called by asio when our timeout expires */
static void timer_cb(const boost::system::error_code & error, GlobalInfo *g) static void timer_cb(const boost::system::error_code & error, GlobalInfo *g)
{ {
if(!error) if(!error) {
{
fprintf(MSG_OUT, "\ntimer_cb: "); fprintf(MSG_OUT, "\ntimer_cb: ");
CURLMcode rc; CURLMcode rc;
rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0, &g->still_running); rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0,
&g->still_running);
mcode_or_die("timer_cb: curl_multi_socket_action", rc); mcode_or_die("timer_cb: curl_multi_socket_action", rc);
check_multi_info(g); check_multi_info(g);
@ -239,22 +231,21 @@ static void remsock(int *f, GlobalInfo *g)
{ {
fprintf(MSG_OUT, "\nremsock: "); fprintf(MSG_OUT, "\nremsock: ");
if(f) if(f) {
{
free(f); free(f);
} }
} }
static void setsock(int *fdp, curl_socket_t s, CURL*e, int act, int oldact, GlobalInfo*g) static void setsock(int *fdp, curl_socket_t s, CURL *e, int act, int oldact,
GlobalInfo *g)
{ {
fprintf(MSG_OUT, "\nsetsock: socket=%d, act=%d, fdp=%p", s, act, fdp); fprintf(MSG_OUT, "\nsetsock: socket=%d, act=%d, fdp=%p", s, act, fdp);
std::map<curl_socket_t, boost::asio::ip::tcp::socket *>::iterator it = socket_map.find(s); std::map<curl_socket_t, boost::asio::ip::tcp::socket *>::iterator it =
socket_map.find(s);
if(it == socket_map.end()) if(it == socket_map.end()) {
{
fprintf(MSG_OUT, "\nsocket %d is a c-ares socket, ignoring", s); fprintf(MSG_OUT, "\nsocket %d is a c-ares socket, ignoring", s);
return; return;
} }
@ -262,36 +253,33 @@ static void setsock(int *fdp, curl_socket_t s, CURL*e, int act, int oldact, Glob
*fdp = act; *fdp = act;
if(act == CURL_POLL_IN) if(act == CURL_POLL_IN) {
{
fprintf(MSG_OUT, "\nwatching for socket to become readable"); fprintf(MSG_OUT, "\nwatching for socket to become readable");
if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT) if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT) {
{
tcp_socket->async_read_some(boost::asio::null_buffers(), tcp_socket->async_read_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket, CURL_POLL_IN, _1, fdp)); boost::bind(&event_cb, g, tcp_socket,
CURL_POLL_IN, _1, fdp));
} }
} }
else if (act == CURL_POLL_OUT) else if(act == CURL_POLL_OUT) {
{
fprintf(MSG_OUT, "\nwatching for socket to become writable"); fprintf(MSG_OUT, "\nwatching for socket to become writable");
if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT) if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT) {
{
tcp_socket->async_write_some(boost::asio::null_buffers(), tcp_socket->async_write_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket, CURL_POLL_OUT, _1, fdp)); boost::bind(&event_cb, g, tcp_socket,
CURL_POLL_OUT, _1, fdp));
} }
} }
else if(act == CURL_POLL_INOUT) else if(act == CURL_POLL_INOUT) {
{
fprintf(MSG_OUT, "\nwatching for socket to become readable & writable"); fprintf(MSG_OUT, "\nwatching for socket to become readable & writable");
if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT) if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT) {
{
tcp_socket->async_read_some(boost::asio::null_buffers(), tcp_socket->async_read_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket, CURL_POLL_IN, _1, fdp)); boost::bind(&event_cb, g, tcp_socket,
CURL_POLL_IN, _1, fdp));
} }
if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT) if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT) {
{
tcp_socket->async_write_some(boost::asio::null_buffers(), tcp_socket->async_write_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket, CURL_POLL_OUT, _1, fdp)); boost::bind(&event_cb, g, tcp_socket,
CURL_POLL_OUT, _1, fdp));
} }
} }
} }
@ -317,20 +305,16 @@ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
fprintf(MSG_OUT, fprintf(MSG_OUT,
"\nsocket callback: s=%d e=%p what=%s ", s, e, whatstr[what]); "\nsocket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
if(what == CURL_POLL_REMOVE) if(what == CURL_POLL_REMOVE) {
{
fprintf(MSG_OUT, "\n"); fprintf(MSG_OUT, "\n");
remsock(actionp, g); remsock(actionp, g);
} }
else else {
{ if(!actionp) {
if(!actionp)
{
fprintf(MSG_OUT, "\nAdding data: %s", whatstr[what]); fprintf(MSG_OUT, "\nAdding data: %s", whatstr[what]);
addsock(s, e, what, g); addsock(s, e, what, g);
} }
else else {
{
fprintf(MSG_OUT, fprintf(MSG_OUT,
"\nChanging action from %s to %s", "\nChanging action from %s to %s",
whatstr[*actionp], whatstr[what]); whatstr[*actionp], whatstr[what]);
@ -344,7 +328,6 @@ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
/* CURLOPT_WRITEFUNCTION */ /* CURLOPT_WRITEFUNCTION */
static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data) static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
{ {
size_t written = size * nmemb; size_t written = size * nmemb;
char *pBuffer = (char *)malloc(written + 1); char *pBuffer = (char *)malloc(written + 1);
@ -382,28 +365,28 @@ static curl_socket_t opensocket(void *clientp, curlsocktype purpose,
curl_socket_t sockfd = CURL_SOCKET_BAD; curl_socket_t sockfd = CURL_SOCKET_BAD;
/* restrict to IPv4 */ /* restrict to IPv4 */
if(purpose == CURLSOCKTYPE_IPCXN && address->family == AF_INET) if(purpose == CURLSOCKTYPE_IPCXN && address->family == AF_INET) {
{
/* create a tcp socket object */ /* create a tcp socket object */
boost::asio::ip::tcp::socket *tcp_socket = new boost::asio::ip::tcp::socket(io_service); boost::asio::ip::tcp::socket *tcp_socket =
new boost::asio::ip::tcp::socket(io_service);
/* open it and get the native handle*/ /* open it and get the native handle*/
boost::system::error_code ec; boost::system::error_code ec;
tcp_socket->open(boost::asio::ip::tcp::v4(), ec); tcp_socket->open(boost::asio::ip::tcp::v4(), ec);
if(ec) if(ec) {
{
/* An error occurred */ /* An error occurred */
std::cout << std::endl << "Couldn't open socket [" << ec << "][" << ec.message() << "]"; std::cout << std::endl << "Couldn't open socket [" << ec << "][" <<
ec.message() << "]";
fprintf(MSG_OUT, "\nERROR: Returning CURL_SOCKET_BAD to signal error"); fprintf(MSG_OUT, "\nERROR: Returning CURL_SOCKET_BAD to signal error");
} }
else else {
{
sockfd = tcp_socket->native_handle(); sockfd = tcp_socket->native_handle();
fprintf(MSG_OUT, "\nOpened socket %d", sockfd); fprintf(MSG_OUT, "\nOpened socket %d", sockfd);
/* save it for monitoring */ /* save it for monitoring */
socket_map.insert(std::pair<curl_socket_t, boost::asio::ip::tcp::socket *>(sockfd, tcp_socket)); socket_map.insert(std::pair<curl_socket_t,
boost::asio::ip::tcp::socket *>(sockfd, tcp_socket));
} }
} }
@ -415,10 +398,10 @@ static int close_socket(void *clientp, curl_socket_t item)
{ {
fprintf(MSG_OUT, "\nclose_socket : %d", item); fprintf(MSG_OUT, "\nclose_socket : %d", item);
std::map<curl_socket_t, boost::asio::ip::tcp::socket *>::iterator it = socket_map.find(item); std::map<curl_socket_t, boost::asio::ip::tcp::socket *>::iterator it =
socket_map.find(item);
if(it != socket_map.end()) if(it != socket_map.end()) {
{
delete it->second; delete it->second;
socket_map.erase(it); socket_map.erase(it);
} }
@ -435,10 +418,8 @@ static void new_conn(char *url, GlobalInfo *g)
conn = (ConnInfo *) calloc(1, sizeof(ConnInfo)); conn = (ConnInfo *) calloc(1, sizeof(ConnInfo));
conn->easy = curl_easy_init(); conn->easy = curl_easy_init();
if(!conn->easy) if(!conn->easy) {
{
fprintf(MSG_OUT, "\ncurl_easy_init() failed, exiting!"); fprintf(MSG_OUT, "\ncurl_easy_init() failed, exiting!");
exit(2); exit(2);
} }

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -22,14 +22,14 @@
/* <DESC> /* <DESC>
* Get a web page, extract the title with libxml. * Get a web page, extract the title with libxml.
* </DESC> * </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
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 <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -90,50 +90,38 @@ static bool init(CURL *&conn, char *url)
conn = curl_easy_init(); conn = curl_easy_init();
if (conn == NULL) if(conn == NULL) {
{
fprintf(stderr, "Failed to create CURL connection\n"); fprintf(stderr, "Failed to create CURL connection\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
code = curl_easy_setopt(conn, CURLOPT_ERRORBUFFER, errorBuffer); code = curl_easy_setopt(conn, CURLOPT_ERRORBUFFER, errorBuffer);
if (code != CURLE_OK) if(code != CURLE_OK) {
{
fprintf(stderr, "Failed to set error buffer [%d]\n", code); fprintf(stderr, "Failed to set error buffer [%d]\n", code);
return false; return false;
} }
code = curl_easy_setopt(conn, CURLOPT_URL, url); code = curl_easy_setopt(conn, CURLOPT_URL, url);
if (code != CURLE_OK) if(code != CURLE_OK) {
{
fprintf(stderr, "Failed to set URL [%s]\n", errorBuffer); fprintf(stderr, "Failed to set URL [%s]\n", errorBuffer);
return false; return false;
} }
code = curl_easy_setopt(conn, CURLOPT_FOLLOWLOCATION, 1L); code = curl_easy_setopt(conn, CURLOPT_FOLLOWLOCATION, 1L);
if (code != CURLE_OK) if(code != CURLE_OK) {
{
fprintf(stderr, "Failed to set redirect option [%s]\n", errorBuffer); fprintf(stderr, "Failed to set redirect option [%s]\n", errorBuffer);
return false; return false;
} }
code = curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, writer); code = curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, writer);
if (code != CURLE_OK) if(code != CURLE_OK) {
{
fprintf(stderr, "Failed to set writer [%s]\n", errorBuffer); fprintf(stderr, "Failed to set writer [%s]\n", errorBuffer);
return false; return false;
} }
code = curl_easy_setopt(conn, CURLOPT_WRITEDATA, &buffer); code = curl_easy_setopt(conn, CURLOPT_WRITEDATA, &buffer);
if (code != CURLE_OK) if(code != CURLE_OK) {
{
fprintf(stderr, "Failed to set write data [%s]\n", errorBuffer); fprintf(stderr, "Failed to set write data [%s]\n", errorBuffer);
return false; return false;
} }
@ -150,8 +138,7 @@ static void StartElement(void *voidContext,
{ {
Context *context = (Context *)voidContext; Context *context = (Context *)voidContext;
if (COMPARE((char *)name, "TITLE")) if(COMPARE((char *)name, "TITLE")) {
{
context->title = ""; context->title = "";
context->addTitle = true; context->addTitle = true;
} }
@ -273,10 +260,8 @@ int main(int argc, char *argv[])
// Ensure one argument is given // Ensure one argument is given
if (argc != 2) if(argc != 2) {
{
fprintf(stderr, "Usage: %s <url>\n", argv[0]); fprintf(stderr, "Usage: %s <url>\n", argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -284,10 +269,8 @@ int main(int argc, char *argv[])
// Initialize CURL connection // Initialize CURL connection
if (!init(conn, argv[1])) if(!init(conn, argv[1])) {
{
fprintf(stderr, "Connection initializion failed\n"); fprintf(stderr, "Connection initializion failed\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -296,19 +279,15 @@ int main(int argc, char *argv[])
code = curl_easy_perform(conn); code = curl_easy_perform(conn);
curl_easy_cleanup(conn); curl_easy_cleanup(conn);
if (code != CURLE_OK) if(code != CURLE_OK) {
{
fprintf(stderr, "Failed to get '%s' [%s]\n", argv[1], errorBuffer); fprintf(stderr, "Failed to get '%s' [%s]\n", argv[1], errorBuffer);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Parse the (assumed) HTML code // Parse the (assumed) HTML code
parseHtml(buffer, title); parseHtml(buffer, title);
// Display the extracted title // Display the extracted title
printf("Title: %s\n", title.c_str()); printf("Title: %s\n", title.c_str());
return EXIT_SUCCESS; return EXIT_SUCCESS;