Moved curl stuff from indra/aistatemachine to indra/llmessage.

This commit is contained in:
Aleric Inglewood
2012-10-31 18:51:52 +01:00
parent 0b265320a9
commit 7549b471c3
11 changed files with 9 additions and 9 deletions

View File

@@ -18,24 +18,15 @@ include_directories(
)
set(aistatemachine_SOURCE_FILES
debug_libcurl.cpp
aistatemachine.cpp
aitimer.cpp
aicurl.cpp
aicurlthread.cpp
aicurleasyrequeststatemachine.cpp
)
set(aistatemachine_HEADER_FILES
CMakeLists.txt
debug_libcurl.h
aistatemachine.h
aitimer.h
aicurlprivate.h
aicurl.h
aicurlthread.h
aicurleasyrequeststatemachine.h
)
set_source_files_properties(${aistatemachine_HEADER_FILES}

File diff suppressed because it is too large Load Diff

View File

@@ -1,547 +0,0 @@
/**
* @file aicurl.h
* @brief Thread safe wrapper for libcurl.
*
* Copyright (c) 2012, Aleric Inglewood.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* There are special exceptions to the terms and conditions of the GPL as
* it is applied to this Source Code. View the full text of the exception
* in the file doc/FLOSS-exception.txt in this software distribution.
*
* CHANGELOG
* and additional copyright holders.
*
* 17/03/2012
* Initial version, written by Aleric Inglewood @ SL
*/
#ifndef AICURL_H
#define AICURL_H
#include <string>
#include <vector>
#include <set>
#include <stdexcept>
#include <boost/intrusive_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/utility.hpp>
#include "llpreprocessor.h"
#include <curl/curl.h> // Needed for files that include this header (also for aicurlprivate.h).
#ifdef DEBUG_CURLIO
#include "debug_libcurl.h"
#endif
// Make sure we don't use this option: it is not thread-safe.
#undef CURLOPT_DNS_USE_GLOBAL_CACHE
#define CURLOPT_DNS_USE_GLOBAL_CACHE do_not_use_CURLOPT_DNS_USE_GLOBAL_CACHE
#include "stdtypes.h" // U16, S32, U32, F64
#include "llatomic.h" // LLAtomicU32
#include "aithreadsafe.h"
#include "llhttpstatuscodes.h"
#include "aihttpheaders.h"
// Debug Settings.
extern bool gNoVerifySSLCert;
class LLSD;
class LLBufferArray;
class LLChannelDescriptors;
class AIHTTPTimeoutPolicy;
// Some pretty printing for curl easy handle related things:
// Print the lock object related to the current easy handle in every debug output.
#ifdef CWDEBUG
#include <libcwd/buf2str.h>
#include <sstream>
#define DoutCurl(x) do { \
using namespace libcwd; \
std::ostringstream marker; \
marker << (void*)this->get_lockobj(); \
libcw_do.push_marker(); \
libcw_do.marker().assign(marker.str().data(), marker.str().size()); \
libcw_do.inc_indent(2); \
Dout(dc::curl, x); \
libcw_do.dec_indent(2); \
libcw_do.pop_marker(); \
} while(0)
#define DoutCurlEntering(x) do { \
using namespace libcwd; \
std::ostringstream marker; \
marker << (void*)this->get_lockobj(); \
libcw_do.push_marker(); \
libcw_do.marker().assign(marker.str().data(), marker.str().size()); \
libcw_do.inc_indent(2); \
DoutEntering(dc::curl, x); \
libcw_do.dec_indent(2); \
libcw_do.pop_marker(); \
} while(0)
#else // !CWDEBUG
#define DoutCurl(x) Dout(dc::curl, x << " [" << (void*)this->get_lockobj() << ']')
#define DoutCurlEntering(x) DoutEntering(dc::curl, x << " [" << (void*)this->get_lockobj() << ']')
#endif // CWDEBUG
//-----------------------------------------------------------------------------
// Exceptions.
//
// A general curl exception.
//
class AICurlError : public std::runtime_error {
public:
AICurlError(std::string const& message) : std::runtime_error(message) { }
};
class AICurlNoEasyHandle : public AICurlError {
public:
AICurlNoEasyHandle(std::string const& message) : AICurlError(message) { }
};
class AICurlNoMultiHandle : public AICurlError {
public:
AICurlNoMultiHandle(std::string const& message) : AICurlError(message) { }
};
class AICurlNoBody : public AICurlError {
public:
AICurlNoBody(std::string const& message) : AICurlError(message) { }
};
// End Exceptions.
//-----------------------------------------------------------------------------
// Forward declaration.
namespace AICurlInterface { struct TransferInfo; }
// Events generated by AICurlPrivate::CurlResponderBuffer.
struct AICurlResponderBufferEvents {
virtual void received_HTTP_header(void) = 0; // For example "HTTP/1.0 200 OK", the first header of a reply.
virtual void received_header(std::string const& key, std::string const& value) = 0; // Subsequent headers.
virtual void completed_headers(U32 status, std::string const& reason, CURLcode code, AICurlInterface::TransferInfo* info) = 0; // Transaction completed.
};
// Things defined in this namespace are called from elsewhere in the viewer code.
namespace AICurlInterface {
// Output parameter of AICurlPrivate::CurlEasyRequest::getResult.
// Used in XMLRPCResponder.
struct TransferInfo {
TransferInfo() : mSizeDownload(0.0), mTotalTime(0.0), mSpeedDownload(0.0) { }
F64 mSizeDownload;
F64 mTotalTime;
F64 mSpeedDownload;
};
//-----------------------------------------------------------------------------
// Global functions.
// Called to handle changes in Debug Settings.
bool handleCurlConcurrentConnections(LLSD const& newvalue);
bool handleNoVerifySSLCert(LLSD const& newvalue);
// Called once at start of application (from newview/llappviewer.cpp by main thread (before threads are created)),
// with main purpose to initialize curl.
void initCurl(void (*)(void) = NULL);
// Called once at start of application (from LLAppViewer::initThreads), starts AICurlThread.
void startCurlThread(U32 CurlConcurrentConnections, bool NoVerifySSLCert);
// Called once at end of application (from newview/llappviewer.cpp by main thread),
// with purpose to stop curl threads, free curl resources and deinitialize curl.
void cleanupCurl(void);
// Called from indra/llmessage/llurlrequest.cpp to print debug output regarding
// an error code returned by EasyRequest::getResult.
// Just returns curl_easy_strerror(errorcode).
std::string strerror(CURLcode errorcode);
// Called from indra/newview/llfloaterabout.cpp for the About floater, and
// from newview/llappviewer.cpp in behalf of debug output.
// Just returns curl_version().
std::string getVersionString(void);
// Called from newview/llappviewer.cpp (and llcrashlogger/llcrashlogger.cpp) to set
// the Certificate Authority file used to verify HTTPS certs.
void setCAFile(std::string const& file);
// Not called from anywhere.
// Can be used to set the path to the Certificate Authority file.
void setCAPath(std::string const& file);
//-----------------------------------------------------------------------------
// Global classes.
// ResponderBase - base class for all Responders.
//
// The life cycle of classes derived from this class is as follows:
// They are allocated with new on the line where get(), getByteRange() or post() is called,
// and the pointer to the allocated object is then put in a reference counting ResponderPtr.
// This ResponderPtr is passed to CurlResponderBuffer::prepRequest which stores it in its
// member mResponder. Hence, the life time of a Responder is never longer than its
// associated CurlResponderBuffer, however, if everything works correctly, then normally a
// responder is deleted in CurlResponderBuffer::removed_from_multi_handle by setting
// mReponder to NULL.
//
// Note that the lifetime of CurlResponderBuffer is (a bit) shorter than the associated
// CurlEasyRequest (because of the order of base classes of ThreadSafeBufferedCurlEasyRequest)
// and the callbacks, as set by prepRequest, only use those two.
// A callback locks the CurlEasyRequest before actually making the callback, and the
// destruction of CurlResponderBuffer also first locks the CurlEasyRequest, and then revokes
// the callbacks. This assures that a Responder is never used when the objects it uses are
// destructed. Also, if any of those are destructed then the Responder is automatically
// destructed too.
//
class ResponderBase : public AICurlResponderBufferEvents {
public:
typedef boost::shared_ptr<LLBufferArray> buffer_ptr_t;
protected:
ResponderBase(void);
virtual ~ResponderBase();
// Read body from buffer and put it into content. If status indicates success, interpret it as LLSD, otherwise copy it as-is.
void decode_llsd_body(U32 status, std::string const& reason, LLChannelDescriptors const& channels, buffer_ptr_t const& buffer, LLSD& content);
// Read body from buffer and put it into content. Always copy it as-is.
void decode_raw_body(U32 status, std::string const& reason, LLChannelDescriptors const& channels, buffer_ptr_t const& buffer, std::string& content);
protected:
// Associated URL, used for debug output.
std::string mURL;
// Headers received from the server.
AIHTTPReceivedHeaders mReceivedHeaders;
// The curl result code.
CURLcode mCode;
// Set when the transaction finished (with or without errors).
bool mFinished;
public:
// Called to set the URL of the current request for this Responder,
// used only when printing debug output regarding activity of the Responder.
void setURL(std::string const& url);
// Accessors.
std::string const& getURL(void) const { return mURL; }
CURLcode result_code(void) const { return mCode; }
// Called by CurlResponderBuffer::timed_out or CurlResponderBuffer::processOutput.
virtual void finished(CURLcode code, U32 http_status, std::string const& reason, LLChannelDescriptors const& channels, buffer_ptr_t const& buffer) = 0;
// Return true if the curl thread is done with this transaction.
// If this returns true then it is guaranteed that none of the
// virtual functions will be called anymore: the curl thread
// will not access this object anymore.
// Note that normally you don't need to call this function.
bool is_finished(void) const { return mFinished; }
protected:
// AICurlResponderBufferEvents
// Called when the "HTTP/1.x <status> <reason>" header is received.
/*virtual*/ void received_HTTP_header(void)
{
// It's possible that this page was moved (302), so we already saw headers
// from the 302 page and are starting over on the new page now.
mReceivedHeaders.clear();
}
// Called for all remaining headers.
/*virtual*/ void received_header(std::string const& key, std::string const& value)
{
mReceivedHeaders.addHeader(key, value);
}
// Called when the whole transaction is completed (also the body was received), but before the body is processed.
/*virtual*/ void completed_headers(U32 status, std::string const& reason, CURLcode code, TransferInfo* info)
{
completedHeaders(status, reason, mReceivedHeaders);
}
public:
// Derived classes that implement completed_headers()/completedHeaders() should return true here.
virtual bool needsHeaders(void) const { return false; }
// A derived class should return true if curl should follow redirections.
// The default is not to follow redirections.
virtual bool followRedir(void) { return false; }
// Timeout policy to use.
virtual AIHTTPTimeoutPolicy const& getHTTPTimeoutPolicy(void) const = 0;
protected:
// Derived classes can override this to get the HTML headers that were received, when the message is completed.
virtual void completedHeaders(U32 status, std::string const& reason, AIHTTPReceivedHeaders const& headers)
{
// The default does nothing.
}
private:
// Used by ResponderPtr. Object is deleted when reference count reaches zero.
LLAtomicU32 mReferenceCount;
friend void intrusive_ptr_add_ref(ResponderBase* p); // Called by boost::intrusive_ptr when a new copy of a boost::intrusive_ptr<ResponderBase> is made.
friend void intrusive_ptr_release(ResponderBase* p); // Called by boost::intrusive_ptr when a boost::intrusive_ptr<ResponderBase> is destroyed.
// This function must delete the ResponderBase object when the reference count reaches zero.
};
// ResponderWithCompleted - base class for Responders that implement completed, or completedRaw if the response is not LLSD.
class ResponderWithCompleted : public ResponderBase {
protected:
// ResponderBase event
// The responder finished. Do not override this function in derived classes; override completedRaw instead.
/*virtual*/ void finished(CURLcode code, U32 http_status, std::string const& reason, LLChannelDescriptors const& channels, buffer_ptr_t const& buffer)
{
mCode = code;
// Allow classes derived from ResponderBase to override completedRaw
// (if not they should override completed or be derived from Responder instead).
completedRaw(http_status, reason, channels, buffer);
mFinished = true;
}
protected:
// Events generated by this class.
// Derived classes can override this to get the raw data of the body of the HTML message that was received.
// The default is to interpret the content as LLSD and call completed().
virtual void completedRaw(U32 status, std::string const& reason, LLChannelDescriptors const& channels, buffer_ptr_t const& buffer);
// ... or, derived classes can override this to get LLSD content when the message is completed.
// The default aborts, as it should never be called (can't make it pure virtual though, so
// classes that override completedRaw don't need to implement this function, too).
virtual void completed(U32 status, std::string const& reason, LLSD const& content);
#ifdef SHOW_ASSERT
// Responders derived from this class must override either completedRaw or completed.
// They may not attempt to override any of the virual functions defined by ResponderBase.
// Define those functions here with different parameters in order to cause a compile
// warning when a class accidently tries to override them.
enum YOU_ARE_DERIVING_FROM_THE_WRONG_CLASS { };
virtual void result(YOU_ARE_DERIVING_FROM_THE_WRONG_CLASS) { }
virtual void errorWithContent(YOU_ARE_DERIVING_FROM_THE_WRONG_CLASS) { }
virtual void error(YOU_ARE_DERIVING_FROM_THE_WRONG_CLASS) { }
#endif
};
// Responder - base class for reponders that expect LLSD in the body of the reply.
//
// Classes derived from Responder must implement result, and either errorWithContent or error.
class Responder : public ResponderBase {
protected:
// The responder finished. Do not override this function in derived classes; use ResponderWithCompleted instead.
/*virtual*/ void finished(CURLcode code, U32 http_status, std::string const& reason, LLChannelDescriptors const& channels, buffer_ptr_t const& buffer);
protected:
// Events generated by this class.
// Derived classes must override this to receive the content of a body upon success.
virtual void result(LLSD const& content) = 0;
// Derived classes can override this to get informed when a bad HTML status code is received.
// The default calls error().
virtual void errorWithContent(U32 status, std::string const& reason, LLSD const& content);
// ... or, derived classes can override this to get informed when a bad HTML status code is received.
// The default prints the error to llinfos.
virtual void error(U32 status, std::string const& reason);
public:
// Called from LLSDMessage::ResponderAdapter::listener.
// LLSDMessage::ResponderAdapter is a hack, showing among others by fact that it needs these functions.
void pubErrorWithContent(CURLcode code, U32 status, std::string const& reason, LLSD const& content) { mCode = code; errorWithContent(status, reason, content); mFinished = true; }
void pubResult(LLSD const& content) { mCode = CURLE_OK; result(content); mFinished = true; }
#ifdef SHOW_ASSERT
// Responders derived from this class must override result, and either errorWithContent or error.
// They may not attempt to override any of the virual functions defined by ResponderWithCompleted.
// Define those functions here with different parameter in order to cause a compile
// warning when a class accidently tries to override them.
enum YOU_ARE_DERIVING_FROM_THE_WRONG_CLASS { };
virtual void completedRaw(YOU_ARE_DERIVING_FROM_THE_WRONG_CLASS) { }
virtual void completed(YOU_ARE_DERIVING_FROM_THE_WRONG_CLASS) { }
#endif
};
// A Responder is passed around as ResponderPtr, which causes it to automatically
// destruct when there are no pointers left pointing to it.
typedef boost::intrusive_ptr<ResponderBase> ResponderPtr;
// Same as above except that this class stores the result, allowing old polling
// code to poll if the transaction finished by calling is_finished() (from the
// main the thread) and then access the results-- as opposed to immediately
// digesting the results when any of the virtual functions are called.
class LegacyPolledResponder : public ResponderWithCompleted {
protected:
U32 mStatus;
std::string mReason;
protected:
// The responder finished. Do not override this function in derived classes.
/*virtual*/ void finished(CURLcode code, U32 http_status, std::string const& reason, LLChannelDescriptors const& channels, buffer_ptr_t const& buffer)
{
mStatus = http_status;
mReason = reason;
// Call base class implementation.
ResponderWithCompleted::finished(code, http_status, reason, channels, buffer);
}
public:
LegacyPolledResponder(void) : mStatus(HTTP_INTERNAL_ERROR) { }
// Accessors.
U32 http_status(void) const { return mStatus; }
std::string const& reason(void) const { return mReason; }
};
} // namespace AICurlInterface
// Forward declaration (see aicurlprivate.h).
namespace AICurlPrivate {
class CurlEasyRequest;
} // namespace AICurlPrivate
// Define access types (_crat = Const Read Access Type, _rat = Read Access Type, _wat = Write Access Type).
// Typical usage is:
// AICurlEasyRequest h1; // Create easy handle.
// AICurlEasyRequest h2(h1); // Make lightweight copies.
// AICurlEasyRequest_wat h2_w(*h2); // Lock and obtain write access to the easy handle.
// Use *h2_w, which is a reference to the locked CurlEasyRequest instance.
// Note: As it is not allowed to use curl easy handles in any way concurrently,
// read access would at most give access to a CURL const*, which will turn out
// to be completely useless; therefore it is sufficient and efficient to use
// an AIThreadSafeSimple and it's unlikely that AICurlEasyRequest_rat will be used.
typedef AIAccessConst<AICurlPrivate::CurlEasyRequest> AICurlEasyRequest_rat;
typedef AIAccess<AICurlPrivate::CurlEasyRequest> AICurlEasyRequest_wat;
// Events generated by AICurlPrivate::CurlEasyHandle.
struct AICurlEasyHandleEvents {
// Events.
virtual void added_to_multi_handle(AICurlEasyRequest_wat& curl_easy_request_w) = 0;
virtual void finished(AICurlEasyRequest_wat& curl_easy_request_w) = 0;
virtual void removed_from_multi_handle(AICurlEasyRequest_wat& curl_easy_request_w) = 0;
// Avoid compiler warning.
virtual ~AICurlEasyHandleEvents() { }
};
// Pointer to data we're going to POST.
class AIPostField : public LLThreadSafeRefCount {
protected:
char const* mData;
public:
AIPostField(char const* data) : mData(data) { }
char const* data(void) const { return mData; }
};
// The pointer to the data that we have to POST is passed around as AIPostFieldPtr,
// which causes it to automatically clean up when there are no pointers left
// pointing to it.
typedef LLPointer<AIPostField> AIPostFieldPtr;
#include "aicurlprivate.h"
// AICurlPrivate::CurlEasyRequestPtr, a boost::intrusive_ptr, is no more threadsafe than a
// builtin type, but wrapping it in AIThreadSafe is obviously not going to help here.
// Therefore we use the following trick: we wrap CurlEasyRequestPtr too, and only allow
// read accesses on it.
// AICurlEasyRequest: a thread safe, reference counting, auto-cleaning curl easy handle.
class AICurlEasyRequest {
private:
// Use AICurlEasyRequestStateMachine, not AICurlEasyRequest.
friend class AICurlEasyRequestStateMachine;
// Initial construction is allowed (thread-safe).
// Note: If ThreadSafeCurlEasyRequest() throws then the memory allocated is still freed.
// 'new' never returned however and neither the constructor nor destructor of mCurlEasyRequest is called in this case.
// This might throw AICurlNoEasyHandle.
AICurlEasyRequest(bool buffered) :
mCurlEasyRequest(buffered ? new AICurlPrivate::ThreadSafeBufferedCurlEasyRequest : new AICurlPrivate::ThreadSafeCurlEasyRequest) { }
public:
// Used for storing this object in a standard container (see MultiHandle::add_easy_request).
AICurlEasyRequest(AICurlEasyRequest const& orig) : mCurlEasyRequest(orig.mCurlEasyRequest) { }
// For the rest, only allow read operations.
AIThreadSafeSimple<AICurlPrivate::CurlEasyRequest>& operator*(void) const { llassert(mCurlEasyRequest.get()); return *mCurlEasyRequest; }
AIThreadSafeSimple<AICurlPrivate::CurlEasyRequest>* operator->(void) const { llassert(mCurlEasyRequest.get()); return mCurlEasyRequest.get(); }
AIThreadSafeSimple<AICurlPrivate::CurlEasyRequest>* get(void) const { return mCurlEasyRequest.get(); }
// Returns true if this object points to the same CurlEasyRequest object.
bool operator==(AICurlEasyRequest const& cer) const { return mCurlEasyRequest == cer.mCurlEasyRequest; }
// Returns true if this object points to a different CurlEasyRequest object.
bool operator!=(AICurlEasyRequest const& cer) const { return mCurlEasyRequest != cer.mCurlEasyRequest; }
// Queue this request for insertion in the multi session.
void addRequest(void);
// Queue a command to remove this request from the multi session (or cancel a queued command to add it).
void removeRequest(void);
// Returns true when this AICurlEasyRequest wraps a AICurlPrivate::ThreadSafeBufferedCurlEasyRequest.
bool isBuffered(void) const { return mCurlEasyRequest->isBuffered(); }
private:
// The actual pointer to the ThreadSafeCurlEasyRequest instance.
AICurlPrivate::CurlEasyRequestPtr mCurlEasyRequest;
private:
// Assignment would not be thread-safe; we may create this object and read from it.
// Note: Destruction is implicitly assumed thread-safe, as it would be a logic error to
// destruct it while another thread still needs it, concurrent or not.
AICurlEasyRequest& operator=(AICurlEasyRequest const&) { return *this; }
public:
// Instead of assignment, it might be helpful to use swap.
void swap(AICurlEasyRequest& cer) { mCurlEasyRequest.swap(cer.mCurlEasyRequest); }
public:
// The more exotic member functions of this class, to deal with passing this class
// as CURLOPT_PRIVATE pointer to a curl handle and afterwards restore it.
// For "internal use" only; don't use things from AICurlPrivate yourself.
// It's thread-safe to give read access the underlaying boost::intrusive_ptr.
// It's not OK to then call get() on that and store the AICurlPrivate::ThreadSafeCurlEasyRequest* separately.
AICurlPrivate::CurlEasyRequestPtr const& get_ptr(void) const { return mCurlEasyRequest; }
// If we have a correct (with regard to reference counting) AICurlPrivate::CurlEasyRequestPtr,
// then it's OK to construct a AICurlEasyRequest from it.
// Note that the external AICurlPrivate::CurlEasyRequestPtr needs its own locking, because
// it's not thread-safe in itself.
AICurlEasyRequest(AICurlPrivate::CurlEasyRequestPtr const& ptr) : mCurlEasyRequest(ptr) { }
// This one is obviously dangerous. It's for use only in MultiHandle::check_run_count.
// See also the long comment in CurlEasyRequest::finalizeRequest with regard to CURLOPT_PRIVATE.
explicit AICurlEasyRequest(AICurlPrivate::ThreadSafeCurlEasyRequest* ptr) : mCurlEasyRequest(ptr) { }
};
// Write Access Type for the buffer.
struct AICurlResponderBuffer_wat : public AIAccess<AICurlPrivate::CurlResponderBuffer> {
explicit AICurlResponderBuffer_wat(AICurlPrivate::ThreadSafeBufferedCurlEasyRequest& lockobj) :
AIAccess<AICurlPrivate::CurlResponderBuffer>(lockobj) { }
AICurlResponderBuffer_wat(AIThreadSafeSimple<AICurlPrivate::CurlEasyRequest>& lockobj) :
AIAccess<AICurlPrivate::CurlResponderBuffer>(static_cast<AICurlPrivate::ThreadSafeBufferedCurlEasyRequest&>(lockobj)) { }
};
#define AICurlPrivate DONTUSE_AICurlPrivate
#endif

View File

@@ -1,276 +0,0 @@
/**
* @file aicurleasyrequeststatemachine.cpp
* @brief Implementation of AICurlEasyRequestStateMachine
*
* Copyright (c) 2012, Aleric Inglewood.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* There are special exceptions to the terms and conditions of the GPL as
* it is applied to this Source Code. View the full text of the exception
* in the file doc/FLOSS-exception.txt in this software distribution.
*
* CHANGELOG
* and additional copyright holders.
*
* 06/05/2012
* Initial version, written by Aleric Inglewood @ SL
*/
#include "linden_common.h"
#include "aicurleasyrequeststatemachine.h"
#include "aihttptimeoutpolicy.h"
#include "llcontrol.h"
enum curleasyrequeststatemachine_state_type {
AICurlEasyRequestStateMachine_addRequest = AIStateMachine::max_state,
AICurlEasyRequestStateMachine_waitAdded,
AICurlEasyRequestStateMachine_added,
AICurlEasyRequestStateMachine_timedOut, // This must be smaller than the rest, so they always overrule.
AICurlEasyRequestStateMachine_finished,
AICurlEasyRequestStateMachine_removed, // The removed states must be largest two, so they are never ignored.
AICurlEasyRequestStateMachine_removed_after_finished
};
char const* AICurlEasyRequestStateMachine::state_str_impl(state_type run_state) const
{
switch(run_state)
{
AI_CASE_RETURN(AICurlEasyRequestStateMachine_addRequest);
AI_CASE_RETURN(AICurlEasyRequestStateMachine_waitAdded);
AI_CASE_RETURN(AICurlEasyRequestStateMachine_added);
AI_CASE_RETURN(AICurlEasyRequestStateMachine_timedOut);
AI_CASE_RETURN(AICurlEasyRequestStateMachine_finished);
AI_CASE_RETURN(AICurlEasyRequestStateMachine_removed);
AI_CASE_RETURN(AICurlEasyRequestStateMachine_removed_after_finished);
}
return "UNKNOWN STATE";
}
void AICurlEasyRequestStateMachine::initialize_impl(void)
{
{
AICurlEasyRequest_wat curlEasyRequest_w(*mCurlEasyRequest);
llassert(curlEasyRequest_w->is_finalized()); // Call finalizeRequest() before calling run().
curlEasyRequest_w->send_events_to(this);
}
mAdded = false;
mTimedOut = false;
mFinished = false;
mHandled = false;
set_state(AICurlEasyRequestStateMachine_addRequest);
}
// CURL-THREAD
void AICurlEasyRequestStateMachine::added_to_multi_handle(AICurlEasyRequest_wat&)
{
set_state(AICurlEasyRequestStateMachine_added);
}
// CURL-THREAD
void AICurlEasyRequestStateMachine::finished(AICurlEasyRequest_wat&)
{
mFinished = true;
set_state(AICurlEasyRequestStateMachine_finished);
}
// CURL-THREAD
void AICurlEasyRequestStateMachine::removed_from_multi_handle(AICurlEasyRequest_wat&)
{
llassert(mFinished || mTimedOut); // If we neither finished nor timed out, then why is this being removed?
// Note that allowing this would cause an assertion later on for removing
// a CurlResponderBuffer with a still active Responder.
set_state(mFinished ? AICurlEasyRequestStateMachine_removed_after_finished : AICurlEasyRequestStateMachine_removed);
}
void AICurlEasyRequestStateMachine::multiplex_impl(void)
{
mSetStateLock.lock();
state_type current_state = mRunState;
mSetStateLock.unlock();
switch (current_state)
{
case AICurlEasyRequestStateMachine_addRequest:
{
set_state(AICurlEasyRequestStateMachine_waitAdded);
idle(AICurlEasyRequestStateMachine_waitAdded); // Wait till AICurlEasyRequestStateMachine::added_to_multi_handle() is called.
// Only AFTER going idle, add request to curl thread; this is needed because calls to set_state() are
// ignored when the statemachine is not idle, and theoretically the callbacks could be called
// immediately after this call.
mAdded = true;
mCurlEasyRequest.addRequest(); // This causes the state to be changed, now or later, to
// AICurlEasyRequestStateMachine_added, then
// AICurlEasyRequestStateMachine_finished and then
// AICurlEasyRequestStateMachine_removed_after_finished.
// The first two states might be skipped thus, and the state at this point is one of
// 1) AICurlEasyRequestStateMachine_waitAdded (idle)
// 2) AICurlEasyRequestStateMachine_added (running)
// 3) AICurlEasyRequestStateMachine_finished (running)
// 4) AICurlEasyRequestStateMachine_removed_after_finished (running)
if (mTotalDelayTimeout > 0.f)
{
// Set an inactivity timer.
// This shouldn't really be necessary, except in the case of a bug
// in libcurl; but lets be sure and set a timer for inactivity.
mTimer = new AIPersistentTimer; // Do not delete timer upon expiration.
mTimer->setInterval(mTotalDelayTimeout);
mTimer->run(this, AICurlEasyRequestStateMachine_timedOut, false, false);
}
break;
}
case AICurlEasyRequestStateMachine_added:
{
// The request was added to the multi handle. This is a no-op, which is good cause
// this state might be skipped anyway ;).
idle(current_state); // Wait for the next event.
// The state at this point is one of
// 1) AICurlEasyRequestStateMachine_added (idle)
// 2) AICurlEasyRequestStateMachine_finished (running)
// 3) AICurlEasyRequestStateMachine_removed_after_finished (running)
break;
}
case AICurlEasyRequestStateMachine_timedOut:
{
// It is possible that exactly at this point the state changes into
// AICurlEasyRequestStateMachine_finished, with as result that mTimedOut
// is set while we will continue with that state. Hence that mTimedOut
// is explicitly reset in that state.
// Libcurl failed to deliver within a reasonable time... Abort operation in order
// to free this curl easy handle and to notify the application that it didn't work.
mTimedOut = true;
llassert(mAdded);
mAdded = false;
mCurlEasyRequest.removeRequest();
idle(current_state); // Wait till AICurlEasyRequestStateMachine::removed_from_multi_handle() is called.
break;
}
case AICurlEasyRequestStateMachine_finished:
case AICurlEasyRequestStateMachine_removed_after_finished:
{
if (!mHandled)
{
// Only do this once.
mHandled = true;
if (mTimer)
{
// Stop the timer. Note that it's the main thread that generates timer events,
// so we're certain that there will be no time out anymore if we reach this point.
mTimer->abort();
}
// The request finished and either data or an error code is available.
if (mBuffered)
{
AICurlEasyRequest_wat easy_request_w(*mCurlEasyRequest);
AICurlResponderBuffer_wat buffered_easy_request_w(*mCurlEasyRequest);
buffered_easy_request_w->processOutput(easy_request_w);
}
}
if (current_state == AICurlEasyRequestStateMachine_finished)
{
idle(current_state); // Wait till AICurlEasyRequestStateMachine::removed_from_multi_handle() is called.
break;
}
// See above.
mTimedOut = false;
/* Fall-Through */
}
case AICurlEasyRequestStateMachine_removed:
{
// The request was removed from the multi handle.
if (mBuffered && mTimedOut)
{
AICurlEasyRequest_wat easy_request_w(*mCurlEasyRequest);
AICurlResponderBuffer_wat buffered_easy_request_w(*mCurlEasyRequest);
buffered_easy_request_w->timed_out();
}
// We're done. If we timed out, abort -- or else the application will
// think that getResult() will return a valid error code from libcurl.
if (mTimedOut)
abort();
else
finish();
break;
}
}
}
void AICurlEasyRequestStateMachine::abort_impl(void)
{
DoutEntering(dc::curl, "AICurlEasyRequestStateMachine::abort_impl() [" << (void*)this << "] [" << (void*)mCurlEasyRequest.get() << "]");
// Revert call to addRequest() if that was already called (and the request wasn't removed again already).
if (mAdded)
{
// Note that it's safe to call this even if the curl thread already removed it, or will removes it
// after we called this, before processing the remove command; only the curl thread calls
// MultiHandle::remove_easy_request, which is a no-op when called twice for the same easy request.
mAdded = false;
mCurlEasyRequest.removeRequest();
}
}
void AICurlEasyRequestStateMachine::finish_impl(void)
{
DoutEntering(dc::curl, "AICurlEasyRequestStateMachine::finish_impl() [" << (void*)this << "] [" << (void*)mCurlEasyRequest.get() << "]");
// Revoke callbacks.
{
AICurlEasyRequest_wat curl_easy_request_w(*mCurlEasyRequest);
if (mBuffered)
{
AICurlResponderBuffer_wat buffered_easy_request_w(*mCurlEasyRequest);
buffered_easy_request_w->send_events_to(NULL);
}
curl_easy_request_w->send_events_to(NULL);
curl_easy_request_w->revokeCallbacks();
}
if (mTimer)
{
// Note that even if the timer expired, it wasn't deleted because we used AIPersistentTimer; so mTimer is still valid.
// Stop the timer, if it's still running.
if (!mHandled)
mTimer->abort();
}
// Auto clean up ourselves.
kill();
}
AICurlEasyRequestStateMachine::AICurlEasyRequestStateMachine(bool buffered) :
mBuffered(buffered), mCurlEasyRequest(buffered), mTimer(NULL), mTotalDelayTimeout(AIHTTPTimeoutPolicy::getDebugSettingsCurlTimeout().getTotalDelay())
{
Dout(dc::statemachine, "Calling AICurlEasyRequestStateMachine(" << (buffered ? "true" : "false") << ") [" << (void*)this << "] [" << (void*)mCurlEasyRequest.get() << "]");
if (!mBuffered)
{
llwarns << "Using unbuffered AICurlEasyRequestStateMachine" << llendl;
}
}
void AICurlEasyRequestStateMachine::setTotalDelayTimeout(F32 totalDelayTimeout)
{
mTotalDelayTimeout = totalDelayTimeout;
}
AICurlEasyRequestStateMachine::~AICurlEasyRequestStateMachine()
{
Dout(dc::statemachine, "Calling ~AICurlEasyRequestStateMachine() [" << (void*)this << "] [" << (void*)mCurlEasyRequest.get() << "]");
}

View File

@@ -1,107 +0,0 @@
/**
* @file aicurleasyrequest.h
* @brief Perform a curl easy request.
*
* Copyright (c) 2012, Aleric Inglewood.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* There are special exceptions to the terms and conditions of the GPL as
* it is applied to this Source Code. View the full text of the exception
* in the file doc/FLOSS-exception.txt in this software distribution.
*
* CHANGELOG
* and additional copyright holders.
*
* 06/05/2012
* Initial version, written by Aleric Inglewood @ SL
*/
#ifndef AICURLEASYREQUEST_H
#define AICURLEASYREQUEST_H
#include "aistatemachine.h"
#include "aitimer.h"
#include "aicurl.h"
// A curl easy request state machine.
//
// Before calling cersm.run() initialize the object (cersm) as follows:
//
// AICurlEasyRequest_wat cersm_w(cersm);
// cersm_w->setopt(...); // etc, see the interface of AICurlPrivate::CurlEasyRequest.
//
// When the state machine finishes, call aborted() to check
// whether or not the statemachine succeeded in fetching
// the URL or not.
//
// Objects of this type can be reused multiple times, see
// also the documentation of AIStateMachine.
//
// Construction of a AICurlEasyRequestStateMachine might throw AICurlNoEasyHandle.
class AICurlEasyRequestStateMachine : public AIStateMachine, public AICurlEasyHandleEvents {
public:
AICurlEasyRequestStateMachine(bool buffered);
// Transparent access.
AICurlEasyRequest mCurlEasyRequest;
private:
bool mBuffered; // Argument used for construction of mCurlEasyRequest.
bool mAdded; // Set when the last command to the curl thread was to add the request.
bool mTimedOut; // Set if the expiration timer timed out.
bool mFinished; // Set by the curl thread to signal it finished.
bool mHandled; // Set when we processed the received data.
AITimer* mTimer; // Expiration timer.
F32 mTotalDelayTimeout; // The time out value for mTimer.
public:
// Called to set a specific time out, instead of the default one.
void setTotalDelayTimeout(F32 totalDelayTimeout);
protected:
// AICurlEasyRequest Events.
// Called when this curl easy handle was added to a multi handle.
/*virtual*/ void added_to_multi_handle(AICurlEasyRequest_wat&);
// Called when this curl easy handle finished processing (right before it is removed from the multi handle).
/*virtual*/ void finished(AICurlEasyRequest_wat&);
// Called after this curl easy handle was removed from a multi handle.
/*virtual*/ void removed_from_multi_handle(AICurlEasyRequest_wat&);
protected:
// AIStateMachine implementations.
// Call finish() (or abort()), not delete.
/*virtual*/ ~AICurlEasyRequestStateMachine();
// Handle initializing the object.
/*virtual*/ void initialize_impl(void);
// Handle mRunState.
/*virtual*/ void multiplex_impl(void);
// Handle aborting from current bs_run state.
/*virtual*/ void abort_impl(void);
// Handle cleaning up from initialization (or post abort) state.
/*virtual*/ void finish_impl(void);
// Implemenation of state_str for run states.
/*virtual*/ char const* state_str_impl(state_type run_state) const;
};
#endif

View File

@@ -1,613 +0,0 @@
/**
* @file aicurlprivate.h
* @brief Thread safe wrapper for libcurl.
*
* Copyright (c) 2012, Aleric Inglewood.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* There are special exceptions to the terms and conditions of the GPL as
* it is applied to this Source Code. View the full text of the exception
* in the file doc/FLOSS-exception.txt in this software distribution.
*
* CHANGELOG
* and additional copyright holders.
*
* 28/04/2012
* Initial version, written by Aleric Inglewood @ SL
*/
#ifndef AICURLPRIVATE_H
#define AICURLPRIVATE_H
#include <sstream>
#include "llatomic.h"
#include "llrefcount.h"
class AIHTTPHeaders;
class AIHTTPTimeoutPolicy;
class AICurlEasyRequestStateMachine;
namespace AICurlPrivate {
class ThreadSafeCurlEasyRequest;
namespace curlthread {
class MultiHandle;
// A class that keeps track of timeout administration per connection.
class HTTPTimeout : public LLRefCount {
private:
AIHTTPTimeoutPolicy const* mPolicy; // A pointer to the used timeout policy.
std::vector<U32> mBuckets; // An array with the number of bytes transfered in each second.
U16 mBucket; // The bucket corresponding to mLastSecond.
bool mNothingReceivedYet; // Set when created, reset when the HTML reply header from the server is received.
bool mLowSpeedOn; // Set while uploading or downloading data.
bool mUploadFinished; // Used to keep track of whether upload_finished was called yet.
S32 mLastSecond; // The time at which lowspeed() was last called, in seconds since mLowSpeedClock.
U32 mTotalBytes; // The sum of all bytes in mBuckets.
U64 mLowSpeedClock; // Clock count at which low speed detection (re)started.
U64 mStalled; // The clock count at which this transaction is considered to be stalling if nothing is transfered anymore.
public:
static F64 const sClockWidth; // Time between two clock ticks in seconds.
static U64 sClockCount; // Clock count used as 'now' during one loop of the main loop.
#if defined(CWDEBUG) || defined(DEBUG_CURLIO)
ThreadSafeCurlEasyRequest* mLockObj;
#endif
public:
HTTPTimeout(AIHTTPTimeoutPolicy const* policy, ThreadSafeCurlEasyRequest* lock_obj) :
mPolicy(policy), mNothingReceivedYet(true), mLowSpeedOn(false), mUploadFinished(false), mStalled((U64)-1)
#if defined(CWDEBUG) || defined(DEBUG_CURLIO)
, mLockObj(lock_obj)
#endif
{ }
// Called after sending all headers, when body data is written the first time.
void connected(void);
// Called when everything we had to send to the server has been sent.
void upload_finished(void);
// Called when data is sent. Returns true if transfer timed out.
bool data_sent(size_t n);
// Called when data is received. Returns true if transfer timed out.
bool data_received(size_t n);
// Called immediately before done() after curl finished, with code.
void done(AICurlEasyRequest_wat const& curlEasyRequest_w, CURLcode code);
// Accessor.
bool has_stalled(void) const { return mStalled < sClockCount; }
// Called from CurlResponderBuffer::processOutput if a timeout occurred.
void print_diagnostics(AICurlEasyRequest_wat const& curlEasyRequest_w);
#if defined(CWDEBUG) || defined(DEBUG_CURLIO)
void* get_lockobj(void) const { return mLockObj; }
#endif
private:
// (Re)start low speed transer rate detection.
void reset_lowspeed(void);
// Common low speed detection, Called from data_sent or data_received.
bool lowspeed(size_t bytes);
};
} // namespace curlthread
struct Stats {
static LLAtomicU32 easy_calls;
static LLAtomicU32 easy_errors;
static LLAtomicU32 easy_init_calls;
static LLAtomicU32 easy_init_errors;
static LLAtomicU32 easy_cleanup_calls;
static LLAtomicU32 multi_calls;
static LLAtomicU32 multi_errors;
static void print(void);
};
void handle_multi_error(CURLMcode code);
inline CURLMcode check_multi_code(CURLMcode code) { Stats::multi_calls++; if (code != CURLM_OK) handle_multi_error(code); return code; }
bool curlThreadIsRunning(void);
void wakeUpCurlThread(void);
void stopCurlThread(void);
class ThreadSafeCurlEasyRequest;
class ThreadSafeBufferedCurlEasyRequest;
#define DECLARE_SETOPT(param_type) \
CURLcode setopt(CURLoption option, param_type parameter)
// This class wraps CURL*'s.
// It guarantees that a pointer is cleaned up when no longer needed, as required by libcurl.
class CurlEasyHandle : public boost::noncopyable, protected AICurlEasyHandleEvents {
public:
CurlEasyHandle(void);
~CurlEasyHandle();
private:
// Disallow assignment.
CurlEasyHandle& operator=(CurlEasyHandle const*);
public:
// Reset all options of a libcurl session handle.
void reset(void) { llassert(!mActiveMultiHandle); curl_easy_reset(mEasyHandle); }
// Set options for a curl easy handle.
DECLARE_SETOPT(long);
DECLARE_SETOPT(long long);
DECLARE_SETOPT(void const*);
DECLARE_SETOPT(curl_debug_callback);
DECLARE_SETOPT(curl_write_callback);
//DECLARE_SETOPT(curl_read_callback); Same type as curl_write_callback
DECLARE_SETOPT(curl_ssl_ctx_callback);
DECLARE_SETOPT(curl_conv_callback);
#if 0 // Not used by the viewer.
DECLARE_SETOPT(curl_progress_callback);
DECLARE_SETOPT(curl_seek_callback);
DECLARE_SETOPT(curl_ioctl_callback);
DECLARE_SETOPT(curl_sockopt_callback);
DECLARE_SETOPT(curl_opensocket_callback);
DECLARE_SETOPT(curl_closesocket_callback);
DECLARE_SETOPT(curl_sshkeycallback);
DECLARE_SETOPT(curl_chunk_bgn_callback);
DECLARE_SETOPT(curl_chunk_end_callback);
DECLARE_SETOPT(curl_fnmatch_callback);
#endif
// Automatically cast int types to a long. Note that U32/S32 are int and
// that you can overload int and long even if they have the same size.
CURLcode setopt(CURLoption option, U32 parameter) { return setopt(option, (long)parameter); }
CURLcode setopt(CURLoption option, S32 parameter) { return setopt(option, (long)parameter); }
// Clone a libcurl session handle using all the options previously set.
//CurlEasyHandle(CurlEasyHandle const& orig);
// URL encode/decode the given string.
char* escape(char* url, int length);
char* unescape(char* url, int inlength , int* outlength);
// Extract information from a curl handle.
private:
CURLcode getinfo_priv(CURLINFO info, void* data) const;
public:
// The rest are inlines to provide some type-safety.
CURLcode getinfo(CURLINFO info, char** data) const { return getinfo_priv(info, data); }
CURLcode getinfo(CURLINFO info, curl_slist** data) const { return getinfo_priv(info, data); }
CURLcode getinfo(CURLINFO info, double* data) const { return getinfo_priv(info, data); }
CURLcode getinfo(CURLINFO info, long* data) const { return getinfo_priv(info, data); }
#ifdef __LP64__ // sizeof(long) > sizeof(int) ?
// Overload for integer types that are too small (libcurl demands a long).
CURLcode getinfo(CURLINFO info, S32* data) const { long ldata; CURLcode res = getinfo_priv(info, &ldata); *data = static_cast<S32>(ldata); return res; }
CURLcode getinfo(CURLINFO info, U32* data) const { long ldata; CURLcode res = getinfo_priv(info, &ldata); *data = static_cast<U32>(ldata); return res; }
#else // sizeof(long) == sizeof(int)
CURLcode getinfo(CURLINFO info, S32* data) const { return getinfo_priv(info, reinterpret_cast<long*>(data)); }
CURLcode getinfo(CURLINFO info, U32* data) const { return getinfo_priv(info, reinterpret_cast<long*>(data)); }
#endif
// Perform a file transfer (blocking).
CURLcode perform(void);
// Pause and unpause a connection.
CURLcode pause(int bitmask);
// Called when a request is queued for removal. In that case a race between the actual removal
// and revoking of the callbacks is harmless (and happens for the raw non-statemachine version).
void remove_queued(void) { mQueuedForRemoval = true; }
// In case it's added after being removed.
void add_queued(void) { mQueuedForRemoval = false; }
private:
CURL* mEasyHandle;
CURLM* mActiveMultiHandle;
mutable char* mErrorBuffer;
AIPostFieldPtr mPostField; // This keeps the POSTFIELD data alive for as long as the easy handle exists.
bool mQueuedForRemoval; // Set if the easy handle is (probably) added to the multi handle, but is queued for removal.
#ifdef SHOW_ASSERT
public:
bool mRemovedPerCommand; // Set if mActiveMultiHandle was reset as per command from the main thread.
#endif
private:
// This should only be called from MultiHandle; add/remove an easy handle to/from a multi handle.
friend class curlthread::MultiHandle;
CURLMcode add_handle_to_multi(AICurlEasyRequest_wat& curl_easy_request_w, CURLM* multi_handle);
CURLMcode remove_handle_from_multi(AICurlEasyRequest_wat& curl_easy_request_w, CURLM* multi_handle);
public:
// Returns true if this easy handle was added to a curl multi handle.
bool active(void) const { return mActiveMultiHandle; }
// Returns true when it is expected that the parent will revoke callbacks before the curl
// easy handle is removed from the multi handle; that usually happens when an external
// error demands termination of the request (ie, an expiration).
bool no_warning(void) const { return mQueuedForRemoval || LLApp::isExiting(); }
// Used for debugging purposes.
bool operator==(CURL* easy_handle) const { return mEasyHandle == easy_handle; }
private:
// Call this prior to every curl_easy function whose return value is passed to check_easy_code.
void setErrorBuffer(void) const;
static void handle_easy_error(CURLcode code);
// Always first call setErrorBuffer()!
static inline CURLcode check_easy_code(CURLcode code)
{
Stats::easy_calls++;
if (code != CURLE_OK)
handle_easy_error(code);
return code;
}
protected:
// Return the underlying curl easy handle.
CURL* getEasyHandle(void) const { return mEasyHandle; }
// Keep POSTFIELD data alive.
void setPostField(AIPostFieldPtr const& post_field_ptr) { mPostField = post_field_ptr; }
private:
// Return, and possibly create, the curl (easy) error buffer used by the current thread.
static char* getTLErrorBuffer(void);
};
// CurlEasyRequest adds a slightly more powerful interface that can be used
// to set the options on a curl easy handle.
//
// Calling sendRequest() will then connect to the given URL and perform
// the data exchange. Use getResult() to determine if an error occurred.
//
// Note that the life cycle of a CurlEasyRequest is controlled by AICurlEasyRequest:
// a CurlEasyRequest is only ever created as base class of a ThreadSafeCurlEasyRequest,
// which is only created by creating a AICurlEasyRequest. When the last copy of such
// AICurlEasyRequest is deleted, then also the ThreadSafeCurlEasyRequest is deleted
// and the CurlEasyRequest destructed.
class CurlEasyRequest : public CurlEasyHandle {
private:
void setPost_raw(U32 size, char const* data);
public:
void setPost(U32 size) { setPost_raw(size, NULL); }
void setPost(AIPostFieldPtr const& postdata, U32 size);
void setPost(char const* data, U32 size) { setPost(new AIPostField(data), size); }
void setoptString(CURLoption option, std::string const& value);
void addHeader(char const* str);
void addHeaders(AIHTTPHeaders const& headers);
private:
// Callback stubs.
static size_t headerCallback(char* ptr, size_t size, size_t nmemb, void* userdata);
static size_t writeCallback(char* ptr, size_t size, size_t nmemb, void* userdata);
static size_t readCallback(char* ptr, size_t size, size_t nmemb, void* userdata);
static CURLcode SSLCtxCallback(CURL* curl, void* sslctx, void* userdata);
curl_write_callback mHeaderCallback;
void* mHeaderCallbackUserData;
curl_write_callback mWriteCallback;
void* mWriteCallbackUserData;
curl_read_callback mReadCallback;
void* mReadCallbackUserData;
curl_ssl_ctx_callback mSSLCtxCallback;
void* mSSLCtxCallbackUserData;
public:
void setHeaderCallback(curl_write_callback callback, void* userdata);
void setWriteCallback(curl_write_callback callback, void* userdata);
void setReadCallback(curl_read_callback callback, void* userdata);
void setSSLCtxCallback(curl_ssl_ctx_callback callback, void* userdata);
// Call this if the set callbacks are about to be invalidated.
void revokeCallbacks(void);
// Reset everything to the state it was in when this object was just created.
void resetState(void);
private:
// Called from applyDefaultOptions.
void applyProxySettings(void);
// Used in applyDefaultOptions.
static CURLcode curlCtxCallback(CURL* curl, void* sslctx, void* parm);
// Called from get_timeout_object and httptimeout.
void create_timeout_object(ThreadSafeCurlEasyRequest* lockobj);
public:
// Set default options that we want applied to all curl easy handles.
void applyDefaultOptions(void);
// Prepare the request for adding it to a multi session, or calling perform.
// This actually adds the headers that were collected with addHeader.
void finalizeRequest(std::string const& url, AIHTTPTimeoutPolicy const& policy, AICurlEasyRequestStateMachine* state_machine);
// Last second initialization. Called from MultiHandle::add_easy_request.
void set_timeout_opts(void);
public:
// Called by MultiHandle::finish_easy_request() to store result code that is returned by getResult.
void storeResult(CURLcode result) { mResult = result; }
// Called by MultiHandle::finish_easy_request() when the curl easy handle is done.
void done(AICurlEasyRequest_wat& curl_easy_request_w, CURLcode result)
{
if (mTimeout)
{
// Update timeout administration.
mTimeout->done(curl_easy_request_w, result);
}
finished(curl_easy_request_w);
}
// Called by in case of an error.
void print_diagnostics(AICurlEasyRequest_wat const& curlEasyRequest_w, CURLcode code);
// Called by MultiHandle::check_run_count() to fill info with the transfer info.
void getTransferInfo(AICurlInterface::TransferInfo* info);
// If result != CURLE_FAILED_INIT then also info was filled.
void getResult(CURLcode* result, AICurlInterface::TransferInfo* info = NULL);
// For debugging purposes.
void print_curl_timings(void) const;
private:
curl_slist* mHeaders;
AICurlEasyHandleEvents* mEventsTarget;
CURLcode mResult; //AIFIXME: this does not belong in the request object, but belongs in the response object.
AIHTTPTimeoutPolicy const* mTimeoutPolicy;
std::string mLowercaseHostname; // Lowercase hostname (canonicalized) extracted from the url.
LLPointer<curlthread::HTTPTimeout> mTimeout;// Timeout administration object associated with last created CurlSocketInfo.
bool mTimeoutIsOrphan; // Set to true when mTimeout is not (yet) associated with a CurlSocketInfo.
#if defined(CWDEBUG) || defined(DEBUG_CURLIO)
public:
bool mDebugIsHeadOrGetMethod;
#endif
public:
// These two are only valid after finalizeRequest.
AIHTTPTimeoutPolicy const* getTimeoutPolicy(void) const { return mTimeoutPolicy; }
std::string const& getLowercaseHostname(void) const { return mLowercaseHostname; }
// Called by CurlSocketInfo to allow access to the last (after a redirect) HTTPTimeout object related to this request.
// This creates mTimeout (unless mTimeoutIsOrphan is set in which case it adopts the orphan).
LLPointer<curlthread::HTTPTimeout>& get_timeout_object(ThreadSafeCurlEasyRequest* lockobj);
// Accessor for mTimeout with optional creation of orphaned object (if lockobj != NULL).
LLPointer<curlthread::HTTPTimeout>& httptimeout(ThreadSafeCurlEasyRequest* lockobj = NULL) { if (lockobj && !mTimeout) create_timeout_object(lockobj); return mTimeout; }
// Return true if no data has been received on the latest socket (if any) for too long.
bool has_stalled(void) const { return mTimeout && mTimeout->has_stalled(); }
private:
// This class may only be created by constructing a ThreadSafeCurlEasyRequest.
friend class ThreadSafeCurlEasyRequest;
// Throws AICurlNoEasyHandle.
CurlEasyRequest(void) : mHeaders(NULL), mEventsTarget(NULL), mResult(CURLE_FAILED_INIT), mTimeoutPolicy(NULL), mTimeoutIsOrphan(false)
#if defined(CWDEBUG) || defined(DEBUG_CURLIO)
, mDebugIsHeadOrGetMethod(false)
#endif
{ applyDefaultOptions(); }
public:
~CurlEasyRequest();
public:
// Post-initialization, set the parent to pass the events to.
void send_events_to(AICurlEasyHandleEvents* target) { mEventsTarget = target; }
// For debugging purposes
bool is_finalized(void) const { return mTimeoutPolicy; }
// Return pointer to the ThreadSafe (wrapped) version of this object.
ThreadSafeCurlEasyRequest* get_lockobj(void);
ThreadSafeCurlEasyRequest const* get_lockobj(void) const;
protected:
// Pass events to parent.
/*virtual*/ void added_to_multi_handle(AICurlEasyRequest_wat& curl_easy_request_w);
/*virtual*/ void finished(AICurlEasyRequest_wat& curl_easy_request_w);
/*virtual*/ void removed_from_multi_handle(AICurlEasyRequest_wat& curl_easy_request_w);
};
// Buffers used by the AICurlInterface::Request API.
// Curl callbacks write into and read from these buffers.
// The interface with the rest of the code is through AICurlInterface::Responder.
//
// The lifetime of a CurlResponderBuffer is slightly shorter than its
// associated CurlEasyRequest; this class can only be created as base class
// of ThreadSafeBufferedCurlEasyRequest, and is therefore constructed after
// the construction of the associated CurlEasyRequest and destructed before it.
// Hence, it's safe to use get_lockobj() and through that access the CurlEasyRequest
// object at all times.
//
// A CurlResponderBuffer is thus created when a ThreadSafeBufferedCurlEasyRequest
// is created which only happens by creating a AICurlEasyRequest(true) instance,
// and when the last AICurlEasyRequest is deleted, then the ThreadSafeBufferedCurlEasyRequest
// is deleted and the CurlResponderBuffer destructed.
class CurlResponderBuffer : protected AICurlResponderBufferEvents, protected AICurlEasyHandleEvents {
public:
typedef AICurlInterface::Responder::buffer_ptr_t buffer_ptr_t;
void resetState(AICurlEasyRequest_wat& curl_easy_request_w);
void prepRequest(AICurlEasyRequest_wat& buffered_curl_easy_request_w, AIHTTPHeaders const& headers, AICurlInterface::ResponderPtr responder);
buffer_ptr_t& getInput(void) { return mInput; }
buffer_ptr_t& getOutput(void) { return mOutput; }
// Called if libcurl doesn't deliver within AIHTTPTimeoutPolicy::mMaximumTotalDelay seconds.
void timed_out(void);
// Called after removed_from_multi_handle was called.
void processOutput(AICurlEasyRequest_wat& curl_easy_request_w);
// Do not write more than this amount.
//void setBodyLimit(U32 size) { mBodyLimit = size; }
// Post-initialization, set the parent to pass the events to.
void send_events_to(AICurlResponderBufferEvents* target) { mEventsTarget = target; }
protected:
// Events from this class.
/*virtual*/ void received_HTTP_header(void);
/*virtual*/ void received_header(std::string const& key, std::string const& value);
/*virtual*/ void completed_headers(U32 status, std::string const& reason, CURLcode code, AICurlInterface::TransferInfo* info);
// CurlEasyHandle events.
/*virtual*/ void added_to_multi_handle(AICurlEasyRequest_wat& curl_easy_request_w);
/*virtual*/ void finished(AICurlEasyRequest_wat& curl_easy_request_w);
/*virtual*/ void removed_from_multi_handle(AICurlEasyRequest_wat& curl_easy_request_w);
private:
buffer_ptr_t mInput;
U8* mLastRead; // Pointer into mInput where we last stopped reading (or NULL to start at the beginning).
buffer_ptr_t mOutput;
AICurlInterface::ResponderPtr mResponder;
//U32 mBodyLimit; // From the old LLURLRequestDetail::mBodyLimit, but never used.
U32 mStatus; // HTTP status, decoded from the first header line.
std::string mReason; // The "reason" from the same header line.
S32 mRequestTransferedBytes;
S32 mResponseTransferedBytes;
AICurlResponderBufferEvents* mEventsTarget;
public:
static LLChannelDescriptors const sChannels; // Channel object for mInput (channel out()) and mOutput (channel in()).
private:
// This class may only be created by constructing a ThreadSafeBufferedCurlEasyRequest.
friend class ThreadSafeBufferedCurlEasyRequest;
CurlResponderBuffer(void);
public:
~CurlResponderBuffer();
private:
static size_t curlWriteCallback(char* data, size_t size, size_t nmemb, void* user_data);
static size_t curlReadCallback(char* data, size_t size, size_t nmemb, void* user_data);
static size_t curlHeaderCallback(char* data, size_t size, size_t nmemb, void* user_data);
// Called from curlHeaderCallback.
void setStatusAndReason(U32 status, std::string const& reason);
public:
// Return pointer to the ThreadSafe (wrapped) version of this object.
ThreadSafeBufferedCurlEasyRequest* get_lockobj(void);
ThreadSafeBufferedCurlEasyRequest const* get_lockobj(void) const;
// Return true when prepRequest was already called and the object has not been
// invalidated as a result of calling timed_out().
bool isValid(void) const { return mResponder; }
};
// This class wraps CurlEasyRequest for thread-safety and adds a reference counter so we can
// copy it around cheaply and it gets destructed automatically when the last instance is deleted.
// It guarantees that the CURL* handle is never used concurrently, which is not allowed by libcurl.
// As AIThreadSafeSimpleDC contains a mutex, it cannot be copied. Therefore we need a reference counter for this object.
class ThreadSafeCurlEasyRequest : public AIThreadSafeSimple<CurlEasyRequest> {
public:
// Throws AICurlNoEasyHandle.
ThreadSafeCurlEasyRequest(void) : mReferenceCount(0)
{ new (ptr()) CurlEasyRequest;
Dout(dc::curl, "Creating ThreadSafeCurlEasyRequest with this = " << (void*)this); }
virtual ~ThreadSafeCurlEasyRequest()
{ Dout(dc::curl, "Destructing ThreadSafeCurlEasyRequest with this = " << (void*)this); }
// Returns true if this is a base class of ThreadSafeBufferedCurlEasyRequest.
virtual bool isBuffered(void) const { return false; }
private:
LLAtomicU32 mReferenceCount;
friend void intrusive_ptr_add_ref(ThreadSafeCurlEasyRequest* p); // Called by boost::intrusive_ptr when a new copy of a boost::intrusive_ptr<ThreadSafeCurlEasyRequest> is made.
friend void intrusive_ptr_release(ThreadSafeCurlEasyRequest* p); // Called by boost::intrusive_ptr when a boost::intrusive_ptr<ThreadSafeCurlEasyRequest> is destroyed.
};
// Same as the above but adds a CurlResponderBuffer. The latter has its own locking in order to
// allow casting the underlying CurlEasyRequest to ThreadSafeCurlEasyRequest, independent of
// what class it is part of: ThreadSafeCurlEasyRequest or ThreadSafeBufferedCurlEasyRequest.
// The virtual destructor of ThreadSafeCurlEasyRequest allows to treat each easy handle transparently
// as a ThreadSafeCurlEasyRequest object, or optionally dynamic_cast it to a ThreadSafeBufferedCurlEasyRequest.
// Note: the order of these base classes is important: AIThreadSafeSimple<CurlResponderBuffer> is now
// destructed before ThreadSafeCurlEasyRequest is.
class ThreadSafeBufferedCurlEasyRequest : public ThreadSafeCurlEasyRequest, public AIThreadSafeSimple<CurlResponderBuffer> {
public:
// Throws AICurlNoEasyHandle.
ThreadSafeBufferedCurlEasyRequest(void) { new (AIThreadSafeSimple<CurlResponderBuffer>::ptr()) CurlResponderBuffer; }
/*virtual*/ bool isBuffered(void) const { return true; }
};
// The curl easy request type wrapped in a reference counting pointer.
typedef boost::intrusive_ptr<AICurlPrivate::ThreadSafeCurlEasyRequest> CurlEasyRequestPtr;
// This class wraps CURLM*'s.
// It guarantees that a pointer is cleaned up when no longer needed, as required by libcurl.
class CurlMultiHandle : public boost::noncopyable {
public:
CurlMultiHandle(void);
~CurlMultiHandle();
private:
// Disallow assignment.
CurlMultiHandle& operator=(CurlMultiHandle const*);
private:
static LLAtomicU32 sTotalMultiHandles;
protected:
CURLM* mMultiHandle;
public:
// Set options for a curl multi handle.
CURLMcode setopt(CURLMoption option, long parameter);
CURLMcode setopt(CURLMoption option, curl_socket_callback parameter);
CURLMcode setopt(CURLMoption option, curl_multi_timer_callback parameter);
CURLMcode setopt(CURLMoption option, void* parameter);
// Returns total number of existing CURLM* handles (excluding ones created outside this class).
static U32 getTotalMultiHandles(void) { return sTotalMultiHandles; }
};
// Overload the setopt methods in order to enforce the correct types (ie, convert an int to a long).
// curl_multi_setopt may only be passed a long,
inline CURLMcode CurlMultiHandle::setopt(CURLMoption option, long parameter)
{
llassert(option == CURLMOPT_MAXCONNECTS || option == CURLMOPT_PIPELINING);
return check_multi_code(curl_multi_setopt(mMultiHandle, option, parameter));
}
// ... or a function pointer,
inline CURLMcode CurlMultiHandle::setopt(CURLMoption option, curl_socket_callback parameter)
{
llassert(option == CURLMOPT_SOCKETFUNCTION);
return check_multi_code(curl_multi_setopt(mMultiHandle, option, parameter));
}
inline CURLMcode CurlMultiHandle::setopt(CURLMoption option, curl_multi_timer_callback parameter)
{
llassert(option == CURLMOPT_TIMERFUNCTION);
return check_multi_code(curl_multi_setopt(mMultiHandle, option, parameter));
}
// ... or an object pointer.
inline CURLMcode CurlMultiHandle::setopt(CURLMoption option, void* parameter)
{
llassert(option == CURLMOPT_SOCKETDATA || option == CURLMOPT_TIMERDATA);
return check_multi_code(curl_multi_setopt(mMultiHandle, option, parameter));
}
} // namespace AICurlPrivate
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -1,139 +0,0 @@
/**
* @file aicurlthread.h
* @brief Thread safe wrapper for libcurl.
*
* Copyright (c) 2012, Aleric Inglewood.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* There are special exceptions to the terms and conditions of the GPL as
* it is applied to this Source Code. View the full text of the exception
* in the file doc/FLOSS-exception.txt in this software distribution.
*
* CHANGELOG
* and additional copyright holders.
*
* 28/04/2012
* Initial version, written by Aleric Inglewood @ SL
*/
#ifndef AICURLTHREAD_H
#define AICURLTHREAD_H
#include "aicurl.h"
#include <vector>
#include <deque>
#undef AICurlPrivate
namespace AICurlPrivate {
namespace curlthread {
class PollSet;
// For ordering a std::set with AICurlEasyRequest objects.
struct AICurlEasyRequestCompare {
bool operator()(AICurlEasyRequest const& h1, AICurlEasyRequest const& h2) { return h1.get() < h2.get(); }
};
//-----------------------------------------------------------------------------
// MultiHandle
// This class adds member functions that will only be called from the AICurlThread thread.
// This class guarantees that all added easy handles will be removed from the multi handle
// before the multi handle is cleaned up, as is required by libcurl.
class MultiHandle : public CurlMultiHandle
{
public:
MultiHandle(void);
~MultiHandle();
// Add/remove an easy handle to/from a multi session.
void add_easy_request(AICurlEasyRequest const& easy_request);
CURLMcode remove_easy_request(AICurlEasyRequest const& easy_request, bool as_per_command = false);
// Reads/writes available data from a particular socket (non-blocking).
CURLMcode socket_action(curl_socket_t sockfd, int ev_bitmask);
// Set data to association with an internal socket.
CURLMcode assign(curl_socket_t sockfd, void* sockptr);
// Read multi stack informationals.
CURLMsg const* info_read(int* msgs_in_queue) const;
private:
typedef std::set<AICurlEasyRequest, AICurlEasyRequestCompare> addedEasyRequests_type;
addedEasyRequests_type mAddedEasyRequests; // All easy requests currently added to the multi handle.
int mRunningHandles; // The last value returned by curl_multi_socket_action.
long mTimeout; // The last timeout in ms as set by the callback CURLMOPT_TIMERFUNCTION.
private:
// Store result and trigger events for easy request.
void finish_easy_request(AICurlEasyRequest const& easy_request, CURLcode result);
// Remove easy request at iter (must exist).
// Note that it's possible that a new request from mQueuedRequests is inserted before iter.
CURLMcode remove_easy_request(addedEasyRequests_type::iterator const& iter, bool as_per_command);
static int socket_callback(CURL* easy, curl_socket_t s, int action, void* userp, void* socketp);
static int timer_callback(CURLM* multi, long timeout_ms, void* userp);
public:
// Returns the number of active easy handles as reported by the last call to curl_multi_socket_action.
int getRunningHandles(void) const { return mRunningHandles; }
// Returns how long to wait for socket action before calling socket_action(CURL_SOCKET_TIMEOUT, 0), in ms.
int getTimeout(void) const { return mTimeout; }
// This is called before sleeping, after calling (one or more times) socket_action.
void check_run_count(void);
// Called from the main loop every time select() timed out.
void handle_stalls(void);
public:
//-----------------------------------------------------------------------------
// Curl socket administration:
PollSet* mReadPollSet;
PollSet* mWritePollSet;
private:
// Temporary throttling hack.
std::deque<AICurlEasyRequest> mQueuedRequests; // Waiting (throttled) requests.
};
} // namespace curlthread
} // namespace AICurlPrivate
// Thread safe, noncopyable curl multi handle.
// This class wraps MultiHandle for thread-safety.
// AIThreadSafeSingleThreadDC cannot be copied, but that is OK as we don't need that (or want that);
// this class provides a thread-local singleton (exactly one instance per thread), and because it
// can't be copied, that guarantees that the CURLM* handle is never used concurrently, which is
// not allowed by libcurl.
class AICurlMultiHandle : public AIThreadSafeSingleThreadDC<AICurlPrivate::curlthread::MultiHandle>, public LLThreadLocalDataMember {
public:
static AICurlMultiHandle& getInstance(void);
static void destroyInstance(void);
private:
// Use getInstance().
AICurlMultiHandle(void) { }
};
typedef AISTAccessConst<AICurlPrivate::curlthread::MultiHandle> AICurlMultiHandle_rat;
typedef AISTAccess<AICurlPrivate::curlthread::MultiHandle> AICurlMultiHandle_wat;
#define AICurlPrivate DONTUSE_AICurlPrivate
#endif

View File

@@ -1,946 +0,0 @@
#ifdef DEBUG_CURLIO
#include "sys.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <stdarg.h>
#include <cstring>
#include "llpreprocessor.h"
#include <curl/curl.h>
#define COMPILING_DEBUG_LIBCURL_CC
#include "debug_libcurl.h"
#include "debug.h"
#include "llerror.h"
#ifdef CWDEBUG
#include <libcwd/buf2str.h>
#endif
#define CURL_VERSION(major, minor, patch) \
(LIBCURL_VERSION_MAJOR > major || \
(LIBCURL_VERSION_MAJOR == major && \
LIBCURL_VERSION_MINOR > minor || \
(LIBCURL_VERSION_MINOR == minor && \
LIBCURL_VERSION_PATCH >= patch)))
static struct curl_slist unchanged_slist;
std::ostream& operator<<(std::ostream& os, struct curl_slist const& slist)
{
struct curl_slist const* ptr = &slist;
if (ptr == &unchanged_slist)
os << " <unchanged> ";
else
{
os << "(curl_slist)@0x" << std::hex << (size_t)ptr << std::dec << "{";
do
{
os << '"' << ptr->data << '"';
ptr = ptr->next;
if (ptr)
os << ", ";
}
while(ptr);
os << "}";
}
return os;
}
#define CASEPRINT(x) case x: os << #x; break
std::ostream& operator<<(std::ostream& os, CURLINFO info)
{
switch (info)
{
CASEPRINT(CURLINFO_EFFECTIVE_URL);
CASEPRINT(CURLINFO_RESPONSE_CODE);
CASEPRINT(CURLINFO_TOTAL_TIME);
CASEPRINT(CURLINFO_NAMELOOKUP_TIME);
CASEPRINT(CURLINFO_CONNECT_TIME);
CASEPRINT(CURLINFO_PRETRANSFER_TIME);
CASEPRINT(CURLINFO_SIZE_UPLOAD);
CASEPRINT(CURLINFO_SIZE_DOWNLOAD);
CASEPRINT(CURLINFO_SPEED_DOWNLOAD);
CASEPRINT(CURLINFO_SPEED_UPLOAD);
CASEPRINT(CURLINFO_HEADER_SIZE);
CASEPRINT(CURLINFO_REQUEST_SIZE);
CASEPRINT(CURLINFO_SSL_VERIFYRESULT);
CASEPRINT(CURLINFO_FILETIME);
CASEPRINT(CURLINFO_CONTENT_LENGTH_DOWNLOAD);
CASEPRINT(CURLINFO_CONTENT_LENGTH_UPLOAD);
CASEPRINT(CURLINFO_STARTTRANSFER_TIME);
CASEPRINT(CURLINFO_CONTENT_TYPE);
CASEPRINT(CURLINFO_REDIRECT_TIME);
CASEPRINT(CURLINFO_REDIRECT_COUNT);
CASEPRINT(CURLINFO_PRIVATE);
CASEPRINT(CURLINFO_HTTP_CONNECTCODE);
CASEPRINT(CURLINFO_HTTPAUTH_AVAIL);
CASEPRINT(CURLINFO_PROXYAUTH_AVAIL);
CASEPRINT(CURLINFO_OS_ERRNO);
CASEPRINT(CURLINFO_NUM_CONNECTS);
CASEPRINT(CURLINFO_SSL_ENGINES);
CASEPRINT(CURLINFO_COOKIELIST);
CASEPRINT(CURLINFO_LASTSOCKET);
CASEPRINT(CURLINFO_FTP_ENTRY_PATH);
CASEPRINT(CURLINFO_REDIRECT_URL);
CASEPRINT(CURLINFO_PRIMARY_IP);
CASEPRINT(CURLINFO_APPCONNECT_TIME);
CASEPRINT(CURLINFO_CERTINFO);
CASEPRINT(CURLINFO_CONDITION_UNMET);
CASEPRINT(CURLINFO_RTSP_SESSION_ID);
CASEPRINT(CURLINFO_RTSP_CLIENT_CSEQ);
CASEPRINT(CURLINFO_RTSP_SERVER_CSEQ);
CASEPRINT(CURLINFO_RTSP_CSEQ_RECV);
CASEPRINT(CURLINFO_PRIMARY_PORT);
CASEPRINT(CURLINFO_LOCAL_IP);
CASEPRINT(CURLINFO_LOCAL_PORT);
default:
os << "<unknown" << " CURLINFO value (" << (int)info << ")>";
}
return os;
}
std::ostream& operator<<(std::ostream& os, CURLcode code)
{
switch(code)
{
CASEPRINT(CURLE_OK);
CASEPRINT(CURLE_UNSUPPORTED_PROTOCOL);
CASEPRINT(CURLE_FAILED_INIT);
CASEPRINT(CURLE_URL_MALFORMAT);
#if CURL_VERSION(7, 21, 5)
CASEPRINT(CURLE_NOT_BUILT_IN);
#endif
CASEPRINT(CURLE_COULDNT_RESOLVE_PROXY);
CASEPRINT(CURLE_COULDNT_RESOLVE_HOST);
CASEPRINT(CURLE_COULDNT_CONNECT);
CASEPRINT(CURLE_FTP_WEIRD_SERVER_REPLY);
CASEPRINT(CURLE_REMOTE_ACCESS_DENIED);
#if 0
CASEPRINT(CURLE_FTP_ACCEPT_FAILED);
#endif
CASEPRINT(CURLE_FTP_WEIRD_PASS_REPLY);
#if 0
CASEPRINT(CURLE_FTP_ACCEPT_TIMEOUT);
#endif
CASEPRINT(CURLE_FTP_WEIRD_PASV_REPLY);
CASEPRINT(CURLE_FTP_WEIRD_227_FORMAT);
CASEPRINT(CURLE_FTP_CANT_GET_HOST);
CASEPRINT(CURLE_OBSOLETE16);
CASEPRINT(CURLE_FTP_COULDNT_SET_TYPE);
CASEPRINT(CURLE_PARTIAL_FILE);
CASEPRINT(CURLE_FTP_COULDNT_RETR_FILE);
CASEPRINT(CURLE_OBSOLETE20);
CASEPRINT(CURLE_QUOTE_ERROR);
CASEPRINT(CURLE_HTTP_RETURNED_ERROR);
CASEPRINT(CURLE_WRITE_ERROR);
CASEPRINT(CURLE_OBSOLETE24);
CASEPRINT(CURLE_UPLOAD_FAILED);
CASEPRINT(CURLE_READ_ERROR);
CASEPRINT(CURLE_OUT_OF_MEMORY);
CASEPRINT(CURLE_OPERATION_TIMEDOUT);
CASEPRINT(CURLE_OBSOLETE29);
CASEPRINT(CURLE_FTP_PORT_FAILED);
CASEPRINT(CURLE_FTP_COULDNT_USE_REST);
CASEPRINT(CURLE_OBSOLETE32);
CASEPRINT(CURLE_RANGE_ERROR);
CASEPRINT(CURLE_HTTP_POST_ERROR);
CASEPRINT(CURLE_SSL_CONNECT_ERROR);
CASEPRINT(CURLE_BAD_DOWNLOAD_RESUME);
CASEPRINT(CURLE_FILE_COULDNT_READ_FILE);
CASEPRINT(CURLE_LDAP_CANNOT_BIND);
CASEPRINT(CURLE_LDAP_SEARCH_FAILED);
CASEPRINT(CURLE_OBSOLETE40);
CASEPRINT(CURLE_FUNCTION_NOT_FOUND);
CASEPRINT(CURLE_ABORTED_BY_CALLBACK);
CASEPRINT(CURLE_BAD_FUNCTION_ARGUMENT);
CASEPRINT(CURLE_OBSOLETE44);
CASEPRINT(CURLE_INTERFACE_FAILED);
CASEPRINT(CURLE_OBSOLETE46);
CASEPRINT(CURLE_TOO_MANY_REDIRECTS );
#if CURL_VERSION(7, 21, 5)
CASEPRINT(CURLE_UNKNOWN_OPTION);
#else
CASEPRINT(CURLE_UNKNOWN_TELNET_OPTION);
#endif
CASEPRINT(CURLE_TELNET_OPTION_SYNTAX );
CASEPRINT(CURLE_OBSOLETE50);
CASEPRINT(CURLE_PEER_FAILED_VERIFICATION);
CASEPRINT(CURLE_GOT_NOTHING);
CASEPRINT(CURLE_SSL_ENGINE_NOTFOUND);
CASEPRINT(CURLE_SSL_ENGINE_SETFAILED);
CASEPRINT(CURLE_SEND_ERROR);
CASEPRINT(CURLE_RECV_ERROR);
CASEPRINT(CURLE_OBSOLETE57);
CASEPRINT(CURLE_SSL_CERTPROBLEM);
CASEPRINT(CURLE_SSL_CIPHER);
CASEPRINT(CURLE_SSL_CACERT);
CASEPRINT(CURLE_BAD_CONTENT_ENCODING);
CASEPRINT(CURLE_LDAP_INVALID_URL);
CASEPRINT(CURLE_FILESIZE_EXCEEDED);
CASEPRINT(CURLE_USE_SSL_FAILED);
CASEPRINT(CURLE_SEND_FAIL_REWIND);
CASEPRINT(CURLE_SSL_ENGINE_INITFAILED);
CASEPRINT(CURLE_LOGIN_DENIED);
CASEPRINT(CURLE_TFTP_NOTFOUND);
CASEPRINT(CURLE_TFTP_PERM);
CASEPRINT(CURLE_REMOTE_DISK_FULL);
CASEPRINT(CURLE_TFTP_ILLEGAL);
CASEPRINT(CURLE_TFTP_UNKNOWNID);
CASEPRINT(CURLE_REMOTE_FILE_EXISTS);
CASEPRINT(CURLE_TFTP_NOSUCHUSER);
CASEPRINT(CURLE_CONV_FAILED);
CASEPRINT(CURLE_CONV_REQD);
CASEPRINT(CURLE_SSL_CACERT_BADFILE);
CASEPRINT(CURLE_REMOTE_FILE_NOT_FOUND);
CASEPRINT(CURLE_SSH);
CASEPRINT(CURLE_SSL_SHUTDOWN_FAILED);
CASEPRINT(CURLE_AGAIN);
CASEPRINT(CURLE_SSL_CRL_BADFILE);
CASEPRINT(CURLE_SSL_ISSUER_ERROR);
CASEPRINT(CURLE_FTP_PRET_FAILED);
CASEPRINT(CURLE_RTSP_CSEQ_ERROR);
CASEPRINT(CURLE_RTSP_SESSION_ERROR);
CASEPRINT(CURLE_FTP_BAD_FILE_LIST);
CASEPRINT(CURLE_CHUNK_FAILED);
default:
os << (code == CURL_LAST ? "<illegal" : "<unknown") << " CURLcode value (" << (int)code << ")>";
}
return os;
}
struct AICURL;
struct AICURLM;
std::ostream& operator<<(std::ostream& os, AICURL* curl)
{
os << "(CURL*)0x" << std::hex << (size_t)curl << std::dec;
return os;
}
std::ostream& operator<<(std::ostream& os, AICURLM* curl)
{
os << "(CURLM*)0x" << std::hex << (size_t)curl << std::dec;
return os;
}
std::ostream& operator<<(std::ostream& os, CURLoption option)
{
switch(option)
{
CASEPRINT(CURLOPT_WRITEDATA);
CASEPRINT(CURLOPT_URL);
CASEPRINT(CURLOPT_PORT);
CASEPRINT(CURLOPT_PROXY);
CASEPRINT(CURLOPT_USERPWD);
CASEPRINT(CURLOPT_PROXYUSERPWD);
CASEPRINT(CURLOPT_RANGE);
CASEPRINT(CURLOPT_READDATA);
CASEPRINT(CURLOPT_ERRORBUFFER);
CASEPRINT(CURLOPT_WRITEFUNCTION);
CASEPRINT(CURLOPT_READFUNCTION);
CASEPRINT(CURLOPT_TIMEOUT);
CASEPRINT(CURLOPT_INFILESIZE);
CASEPRINT(CURLOPT_POSTFIELDS);
CASEPRINT(CURLOPT_REFERER);
CASEPRINT(CURLOPT_FTPPORT);
CASEPRINT(CURLOPT_USERAGENT);
CASEPRINT(CURLOPT_LOW_SPEED_LIMIT);
CASEPRINT(CURLOPT_LOW_SPEED_TIME);
CASEPRINT(CURLOPT_RESUME_FROM);
CASEPRINT(CURLOPT_COOKIE);
CASEPRINT(CURLOPT_HTTPHEADER);
CASEPRINT(CURLOPT_HTTPPOST);
CASEPRINT(CURLOPT_SSLCERT);
CASEPRINT(CURLOPT_KEYPASSWD);
CASEPRINT(CURLOPT_CRLF);
CASEPRINT(CURLOPT_QUOTE);
CASEPRINT(CURLOPT_HEADERDATA);
CASEPRINT(CURLOPT_COOKIEFILE);
CASEPRINT(CURLOPT_SSLVERSION);
CASEPRINT(CURLOPT_TIMECONDITION);
CASEPRINT(CURLOPT_TIMEVALUE);
CASEPRINT(CURLOPT_CUSTOMREQUEST);
CASEPRINT(CURLOPT_STDERR);
CASEPRINT(CURLOPT_POSTQUOTE);
CASEPRINT(CURLOPT_WRITEINFO);
CASEPRINT(CURLOPT_VERBOSE);
CASEPRINT(CURLOPT_HEADER);
CASEPRINT(CURLOPT_NOPROGRESS);
CASEPRINT(CURLOPT_NOBODY);
CASEPRINT(CURLOPT_FAILONERROR);
CASEPRINT(CURLOPT_UPLOAD);
CASEPRINT(CURLOPT_POST);
CASEPRINT(CURLOPT_DIRLISTONLY);
CASEPRINT(CURLOPT_APPEND);
CASEPRINT(CURLOPT_NETRC);
CASEPRINT(CURLOPT_FOLLOWLOCATION);
CASEPRINT(CURLOPT_TRANSFERTEXT);
CASEPRINT(CURLOPT_PUT);
CASEPRINT(CURLOPT_PROGRESSFUNCTION);
CASEPRINT(CURLOPT_PROGRESSDATA);
CASEPRINT(CURLOPT_AUTOREFERER);
CASEPRINT(CURLOPT_PROXYPORT);
CASEPRINT(CURLOPT_POSTFIELDSIZE);
CASEPRINT(CURLOPT_HTTPPROXYTUNNEL);
CASEPRINT(CURLOPT_INTERFACE);
CASEPRINT(CURLOPT_KRBLEVEL);
CASEPRINT(CURLOPT_SSL_VERIFYPEER);
CASEPRINT(CURLOPT_CAINFO);
CASEPRINT(CURLOPT_MAXREDIRS);
CASEPRINT(CURLOPT_FILETIME);
CASEPRINT(CURLOPT_TELNETOPTIONS);
CASEPRINT(CURLOPT_MAXCONNECTS);
CASEPRINT(CURLOPT_CLOSEPOLICY);
CASEPRINT(CURLOPT_FRESH_CONNECT);
CASEPRINT(CURLOPT_FORBID_REUSE);
CASEPRINT(CURLOPT_RANDOM_FILE);
CASEPRINT(CURLOPT_EGDSOCKET);
CASEPRINT(CURLOPT_CONNECTTIMEOUT);
CASEPRINT(CURLOPT_HEADERFUNCTION);
CASEPRINT(CURLOPT_HTTPGET);
CASEPRINT(CURLOPT_SSL_VERIFYHOST);
CASEPRINT(CURLOPT_COOKIEJAR);
CASEPRINT(CURLOPT_SSL_CIPHER_LIST);
CASEPRINT(CURLOPT_HTTP_VERSION);
CASEPRINT(CURLOPT_FTP_USE_EPSV);
CASEPRINT(CURLOPT_SSLCERTTYPE);
CASEPRINT(CURLOPT_SSLKEY);
CASEPRINT(CURLOPT_SSLKEYTYPE);
CASEPRINT(CURLOPT_SSLENGINE);
CASEPRINT(CURLOPT_SSLENGINE_DEFAULT);
CASEPRINT(CURLOPT_DNS_USE_GLOBAL_CACHE);
CASEPRINT(CURLOPT_DNS_CACHE_TIMEOUT);
CASEPRINT(CURLOPT_PREQUOTE);
CASEPRINT(CURLOPT_DEBUGFUNCTION);
CASEPRINT(CURLOPT_DEBUGDATA);
CASEPRINT(CURLOPT_COOKIESESSION);
CASEPRINT(CURLOPT_CAPATH);
CASEPRINT(CURLOPT_BUFFERSIZE);
CASEPRINT(CURLOPT_NOSIGNAL);
CASEPRINT(CURLOPT_SHARE);
CASEPRINT(CURLOPT_PROXYTYPE);
#if CURL_VERSION(7, 21, 6)
CASEPRINT(CURLOPT_ACCEPT_ENCODING);
#else
CASEPRINT(CURLOPT_ENCODING);
#endif
CASEPRINT(CURLOPT_PRIVATE);
CASEPRINT(CURLOPT_HTTP200ALIASES);
CASEPRINT(CURLOPT_UNRESTRICTED_AUTH);
CASEPRINT(CURLOPT_FTP_USE_EPRT);
CASEPRINT(CURLOPT_HTTPAUTH);
CASEPRINT(CURLOPT_SSL_CTX_FUNCTION);
CASEPRINT(CURLOPT_SSL_CTX_DATA);
CASEPRINT(CURLOPT_FTP_CREATE_MISSING_DIRS);
CASEPRINT(CURLOPT_PROXYAUTH);
CASEPRINT(CURLOPT_FTP_RESPONSE_TIMEOUT);
CASEPRINT(CURLOPT_IPRESOLVE);
CASEPRINT(CURLOPT_MAXFILESIZE);
CASEPRINT(CURLOPT_INFILESIZE_LARGE);
CASEPRINT(CURLOPT_RESUME_FROM_LARGE);
CASEPRINT(CURLOPT_MAXFILESIZE_LARGE);
CASEPRINT(CURLOPT_NETRC_FILE);
CASEPRINT(CURLOPT_USE_SSL);
CASEPRINT(CURLOPT_POSTFIELDSIZE_LARGE);
CASEPRINT(CURLOPT_TCP_NODELAY);
CASEPRINT(CURLOPT_FTPSSLAUTH);
CASEPRINT(CURLOPT_IOCTLFUNCTION);
CASEPRINT(CURLOPT_IOCTLDATA);
CASEPRINT(CURLOPT_FTP_ACCOUNT);
CASEPRINT(CURLOPT_COOKIELIST);
CASEPRINT(CURLOPT_IGNORE_CONTENT_LENGTH);
CASEPRINT(CURLOPT_FTP_SKIP_PASV_IP);
CASEPRINT(CURLOPT_FTP_FILEMETHOD);
CASEPRINT(CURLOPT_LOCALPORT);
CASEPRINT(CURLOPT_LOCALPORTRANGE);
CASEPRINT(CURLOPT_CONNECT_ONLY);
CASEPRINT(CURLOPT_CONV_FROM_NETWORK_FUNCTION);
CASEPRINT(CURLOPT_CONV_TO_NETWORK_FUNCTION);
CASEPRINT(CURLOPT_CONV_FROM_UTF8_FUNCTION);
CASEPRINT(CURLOPT_MAX_SEND_SPEED_LARGE);
CASEPRINT(CURLOPT_MAX_RECV_SPEED_LARGE);
CASEPRINT(CURLOPT_FTP_ALTERNATIVE_TO_USER);
CASEPRINT(CURLOPT_SOCKOPTFUNCTION);
CASEPRINT(CURLOPT_SOCKOPTDATA);
CASEPRINT(CURLOPT_SSL_SESSIONID_CACHE);
CASEPRINT(CURLOPT_SSH_AUTH_TYPES);
CASEPRINT(CURLOPT_SSH_PUBLIC_KEYFILE);
CASEPRINT(CURLOPT_SSH_PRIVATE_KEYFILE);
CASEPRINT(CURLOPT_FTP_SSL_CCC);
CASEPRINT(CURLOPT_TIMEOUT_MS);
CASEPRINT(CURLOPT_CONNECTTIMEOUT_MS);
CASEPRINT(CURLOPT_HTTP_TRANSFER_DECODING);
CASEPRINT(CURLOPT_HTTP_CONTENT_DECODING);
CASEPRINT(CURLOPT_NEW_FILE_PERMS);
CASEPRINT(CURLOPT_NEW_DIRECTORY_PERMS);
CASEPRINT(CURLOPT_POSTREDIR);
CASEPRINT(CURLOPT_SSH_HOST_PUBLIC_KEY_MD5);
CASEPRINT(CURLOPT_OPENSOCKETFUNCTION);
CASEPRINT(CURLOPT_OPENSOCKETDATA);
CASEPRINT(CURLOPT_COPYPOSTFIELDS);
CASEPRINT(CURLOPT_PROXY_TRANSFER_MODE);
CASEPRINT(CURLOPT_SEEKFUNCTION);
CASEPRINT(CURLOPT_SEEKDATA);
CASEPRINT(CURLOPT_CRLFILE);
CASEPRINT(CURLOPT_ISSUERCERT);
CASEPRINT(CURLOPT_ADDRESS_SCOPE);
CASEPRINT(CURLOPT_CERTINFO);
CASEPRINT(CURLOPT_USERNAME);
CASEPRINT(CURLOPT_PASSWORD);
CASEPRINT(CURLOPT_PROXYUSERNAME);
CASEPRINT(CURLOPT_PROXYPASSWORD);
CASEPRINT(CURLOPT_NOPROXY);
CASEPRINT(CURLOPT_TFTP_BLKSIZE);
CASEPRINT(CURLOPT_SOCKS5_GSSAPI_SERVICE);
CASEPRINT(CURLOPT_SOCKS5_GSSAPI_NEC);
CASEPRINT(CURLOPT_PROTOCOLS);
CASEPRINT(CURLOPT_REDIR_PROTOCOLS);
CASEPRINT(CURLOPT_SSH_KNOWNHOSTS);
CASEPRINT(CURLOPT_SSH_KEYFUNCTION);
CASEPRINT(CURLOPT_SSH_KEYDATA);
CASEPRINT(CURLOPT_MAIL_FROM);
CASEPRINT(CURLOPT_MAIL_RCPT);
CASEPRINT(CURLOPT_FTP_USE_PRET);
CASEPRINT(CURLOPT_RTSP_REQUEST);
CASEPRINT(CURLOPT_RTSP_SESSION_ID);
CASEPRINT(CURLOPT_RTSP_STREAM_URI);
CASEPRINT(CURLOPT_RTSP_TRANSPORT);
CASEPRINT(CURLOPT_RTSP_CLIENT_CSEQ);
CASEPRINT(CURLOPT_RTSP_SERVER_CSEQ);
CASEPRINT(CURLOPT_INTERLEAVEDATA);
CASEPRINT(CURLOPT_INTERLEAVEFUNCTION);
CASEPRINT(CURLOPT_WILDCARDMATCH);
CASEPRINT(CURLOPT_CHUNK_BGN_FUNCTION);
CASEPRINT(CURLOPT_CHUNK_END_FUNCTION);
CASEPRINT(CURLOPT_FNMATCH_FUNCTION);
CASEPRINT(CURLOPT_CHUNK_DATA);
CASEPRINT(CURLOPT_FNMATCH_DATA);
#if CURL_VERSION(7, 21, 3)
CASEPRINT(CURLOPT_RESOLVE);
#endif
#if CURL_VERSION(7, 21, 4)
CASEPRINT(CURLOPT_TLSAUTH_USERNAME);
CASEPRINT(CURLOPT_TLSAUTH_PASSWORD);
CASEPRINT(CURLOPT_TLSAUTH_TYPE);
#endif
#if CURL_VERSION(7, 21, 6)
CASEPRINT(CURLOPT_TRANSFER_ENCODING);
#endif
#if CURL_VERSION(7, 21, 7)
CASEPRINT(CURLOPT_CLOSESOCKETFUNCTION);
CASEPRINT(CURLOPT_CLOSESOCKETDATA);
#endif
#if CURL_VERSION(7, 22, 0)
CASEPRINT(CURLOPT_GSSAPI_DELEGATION);
#endif
#if CURL_VERSION(7, 24, 0)
CASEPRINT(CURLOPT_DNS_SERVERS);
CASEPRINT(CURLOPT_ACCEPTTIMEOUT_MS);
#endif
#if CURL_VERSION(7, 25, 0)
CASEPRINT(CURLOPT_TCP_KEEPALIVE);
CASEPRINT(CURLOPT_TCP_KEEPIDLE);
CASEPRINT(CURLOPT_TCP_KEEPINTVL);
#endif
#if CURL_VERSION(7, 25, 0)
CASEPRINT(CURLOPT_SSL_OPTIONS);
CASEPRINT(CURLOPT_MAIL_AUTH);
#endif
default:
os << "<unknown CURLoption value (" << (int)option << ")>";
}
return os;
}
std::ostream& operator<<(std::ostream& os, CURLMoption option)
{
switch(option)
{
CASEPRINT(CURLMOPT_SOCKETFUNCTION);
CASEPRINT(CURLMOPT_SOCKETDATA);
CASEPRINT(CURLMOPT_PIPELINING);
CASEPRINT(CURLMOPT_TIMERFUNCTION);
CASEPRINT(CURLMOPT_TIMERDATA);
CASEPRINT(CURLMOPT_MAXCONNECTS);
default:
os << "<unknown CURLMoption value (" << (int)option << ")>";
}
return os;
}
std::ostream& operator<<(std::ostream& os, CURLMsg* msg)
{
if (msg)
{
os << "(CURLMsg*){";
if (msg->msg == CURLMSG_DONE)
os << "CURLMSG_DONE";
else
os << msg->msg;
os << ", " << (AICURL*)msg->easy_handle << ", 0x" << std::hex << (size_t)msg->data.whatever << std::dec << '}';
}
else
os << "(CURLMsg*)NULL";
return os;
}
struct Socket {
curl_socket_t mSocket;
Socket(curl_socket_t sockfd) : mSocket(sockfd) { }
};
std::ostream& operator<<(std::ostream& os, Socket const& sock)
{
if (sock.mSocket == CURL_SOCKET_TIMEOUT)
os << "CURL_SOCKET_TIMEOUT";
else
os << sock.mSocket;
return os;
}
struct EvBitmask {
int mBitmask;
EvBitmask(int mask) : mBitmask(mask) { }
};
std::ostream& operator<<(std::ostream& os, EvBitmask const& bitmask)
{
int m = bitmask.mBitmask;
if (m == 0)
os << '0';
if ((m & CURL_CSELECT_IN))
{
os << "CURL_CSELECT_IN";
if ((m & (CURL_CSELECT_OUT|CURL_CSELECT_ERR)))
os << '|';
}
if ((m & CURL_CSELECT_OUT))
{
os << "CURL_CSELECT_OUT";
if ((m & CURL_CSELECT_ERR))
os << '|';
}
if ((m & CURL_CSELECT_ERR))
{
os << "CURL_CSELECT_ERR";
}
return os;
}
extern "C" {
void debug_curl_easy_cleanup(CURL* handle)
{
curl_easy_cleanup(handle);
Dout(dc::curl, "curl_easy_cleanup(" << (AICURL*)handle << ")");
}
CURL* debug_curl_easy_duphandle(CURL* handle)
{
CURL* ret;
ret = curl_easy_duphandle(handle);
Dout(dc::curl, "curl_easy_duphandle(" << (AICURL*)handle << ") = " << (AICURL*)ret);
return ret;
}
char* debug_curl_easy_escape(CURL* curl, char* url, int length)
{
char* ret;
ret = curl_easy_escape(curl, url, length);
Dout(dc::curl, "curl_easy_escape(" << (AICURL*)curl << ", \"" << url << "\", " << length << ") = \"" << ret << '"');
return ret;
}
CURLcode debug_curl_easy_getinfo(CURL* curl, CURLINFO info, ...)
{
CURLcode ret;
va_list ap;
union param_type {
void* some_ptr;
long* long_ptr;
char** char_ptr;
curl_slist** curl_slist_ptr;
double* double_ptr;
} param;
va_start(ap, info);
param.some_ptr = va_arg(ap, void*);
va_end(ap);
ret = curl_easy_getinfo(curl, info, param.some_ptr);
if (info == CURLINFO_PRIVATE)
{
Dout(dc::curl, "curl_easy_getinfo(" << (AICURL*)curl << ", " << info << ", 0x" << std::hex << (size_t)param.some_ptr << std::dec << ") = " << ret);
}
else
{
switch((info & CURLINFO_TYPEMASK))
{
case CURLINFO_STRING:
Dout(dc::curl, "curl_easy_getinfo(" << (AICURL*)curl << ", " << info << ", (char**){ \"" << (ret == CURLE_OK ? *param.char_ptr : " <unchanged> ") << "\" }) = " << ret);
break;
case CURLINFO_LONG:
Dout(dc::curl, "curl_easy_getinfo(" << (AICURL*)curl << ", " << info << ", (long*){ " << (ret == CURLE_OK ? *param.long_ptr : 0L) << "L }) = " << ret);
break;
case CURLINFO_DOUBLE:
Dout(dc::curl, "curl_easy_getinfo(" << (AICURL*)curl << ", " << info << ", (double*){" << (ret == CURLE_OK ? *param.double_ptr : 0.) << "}) = " << ret);
break;
case CURLINFO_SLIST:
Dout(dc::curl, "curl_easy_getinfo(" << (AICURL*)curl << ", " << info << ", (curl_slist**){ " << (ret == CURLE_OK ? **param.curl_slist_ptr : unchanged_slist) << " }) = " << ret);
break;
}
}
return ret;
}
CURL* debug_curl_easy_init(void)
{
CURL* ret;
ret = curl_easy_init();
Dout(dc::curl, "curl_easy_init() = " << (AICURL*)ret);
return ret;
}
CURLcode debug_curl_easy_pause(CURL* handle, int bitmask)
{
CURLcode ret;
ret = curl_easy_pause(handle, bitmask);
Dout(dc::curl, "curl_easy_pause(" << (AICURL*)handle << ", 0x" << std::hex << bitmask << std::dec << ") = " << ret);
return ret;
}
CURLcode debug_curl_easy_perform(CURL* handle)
{
CURLcode ret;
ret = curl_easy_perform(handle);
Dout(dc::curl, "curl_easy_perform(" << (AICURL*)handle << ") = " << ret);
return ret;
}
void debug_curl_easy_reset(CURL* handle)
{
curl_easy_reset(handle);
Dout(dc::curl, "curl_easy_reset(" << (AICURL*)handle << ")");
}
CURLcode debug_curl_easy_setopt(CURL* handle, CURLoption option, ...)
{
CURLcode ret;
va_list ap;
union param_type {
long along;
void* ptr;
curl_off_t offset;
} param;
unsigned int param_type = (option / 10000) * 10000;
va_start(ap, option);
switch (param_type)
{
case CURLOPTTYPE_LONG:
param.along = va_arg(ap, long);
break;
case CURLOPTTYPE_OBJECTPOINT:
case CURLOPTTYPE_FUNCTIONPOINT:
param.ptr = va_arg(ap, void*);
break;
case CURLOPTTYPE_OFF_T:
param.offset = va_arg(ap, curl_off_t);
break;
default:
std::cerr << "Extracting param_type failed; option = " << option << "; param_type = " << param_type << std::endl;
std::exit(EXIT_FAILURE);
}
va_end(ap);
static long postfieldsize; // Cache. Assumes only one thread sets CURLOPT_POSTFIELDSIZE.
switch (param_type)
{
case CURLOPTTYPE_LONG:
{
ret = curl_easy_setopt(handle, option, param.along);
Dout(dc::curl, "curl_easy_setopt(" << (AICURL*)handle << ", " << option << ", " << param.along << "L) = " << ret);
if (option == CURLOPT_POSTFIELDSIZE)
{
postfieldsize = param.along;
}
break;
}
case CURLOPTTYPE_OBJECTPOINT:
{
ret = curl_easy_setopt(handle, option, param.ptr);
LibcwDoutScopeBegin(LIBCWD_DEBUGCHANNELS, libcwd::libcw_do, dc::curl)
LibcwDoutStream << "curl_easy_setopt(" << (AICURL*)handle << ", " << option << ", ";
// For a subset of all options that take a char*, print the string passed.
if (option == CURLOPT_PROXY || // Set HTTP proxy to use. The parameter should be a char* to a zero terminated string holding the host name or dotted IP address.
option == CURLOPT_PROXYUSERPWD || // Pass a char* as parameter, which should be [user name]:[password] to use for the connection to the HTTP proxy.
option == CURLOPT_CAINFO || // Pass a char * to a zero terminated string naming a file holding one or more certificates to verify the peer with.
option == CURLOPT_URL || // Pass in a pointer to the actual URL to deal with. The parameter should be a char * to a zero terminated string [...]
option == CURLOPT_COOKIEFILE || // Pass a pointer to a zero terminated string as parameter. It should contain the name of your file holding cookie data to read.
option == CURLOPT_CUSTOMREQUEST || // Pass a pointer to a zero terminated string as parameter. It can be used to specify the request instead of GET or HEAD when performing HTTP based requests
option == CURLOPT_ENCODING || // Sets the contents of the Accept-Encoding: header sent in a HTTP request, and enables decoding of a response when a Content-Encoding: header is received.
option == CURLOPT_POSTFIELDS ||
option == CURLOPT_COPYPOSTFIELDS) // Full data to post in a HTTP POST operation.
{
bool const is_postfield = option == CURLOPT_POSTFIELDS || option == CURLOPT_COPYPOSTFIELDS;
char* str = (char*)param.ptr;
long size;
LibcwDoutStream << "(char*)0x" << std::hex << (size_t)param.ptr << std::dec << ")";
if (is_postfield)
{
size = postfieldsize < 32 ? postfieldsize : 32; // Only print first 32 characters (this was already written to debug output before).
}
else
{
size = strlen(str);
}
LibcwDoutStream << "[";
if (str)
{
LibcwDoutStream << libcwd::buf2str(str, size);
if (is_postfield && postfieldsize > 32)
LibcwDoutStream << "...";
}
else
{
LibcwDoutStream << "NULL";
}
LibcwDoutStream << "]";
if (str)
{
LibcwDoutStream << "(" << (is_postfield ? postfieldsize : size) << " bytes))";
}
}
else
{
LibcwDoutStream << "(object*)0x" << std::hex << (size_t)param.ptr << std::dec << ")";
}
LibcwDoutStream << " = " << ret;
LibcwDoutScopeEnd;
if (option == CURLOPT_HTTPHEADER && param.ptr)
{
debug::Indent indent(2);
Dout(dc::curl, "HTTP Headers:");
struct curl_slist* list = (struct curl_slist*)param.ptr;
while (list)
{
Dout(dc::curl, '"' << list->data << '"');
list = list->next;
}
}
break;
}
case CURLOPTTYPE_FUNCTIONPOINT:
ret = curl_easy_setopt(handle, option, param.ptr);
Dout(dc::curl, "curl_easy_setopt(" << (AICURL*)handle << ", " << option << ", (function*)0x" << std::hex << (size_t)param.ptr << std::dec << ") = " << ret);
break;
case CURLOPTTYPE_OFF_T:
{
ret = curl_easy_setopt(handle, option, param.offset);
Dout(dc::curl, "curl_easy_setopt(" << (AICURL*)handle << ", " << option << ", (curl_off_t)" << param.offset << ") = " << ret);
if (option == CURLOPT_POSTFIELDSIZE_LARGE)
{
postfieldsize = (long)param.offset;
}
break;
}
default:
break;
}
return ret;
}
char const* debug_curl_easy_strerror(CURLcode errornum)
{
char const* ret;
ret = curl_easy_strerror(errornum);
Dout(dc::curl, "curl_easy_strerror(" << errornum << ") = \"" << ret << '"');
return ret;
}
char* debug_curl_easy_unescape(CURL* curl, char* url, int inlength, int* outlength)
{
char* ret;
ret = curl_easy_unescape(curl, url, inlength, outlength);
Dout(dc::curl, "curl_easy_unescape(" << (AICURL*)curl << ", \"" << url << "\", " << inlength << ", " << ((ret && outlength) ? *outlength : 1) << ") = \"" << ret << '"');
return ret;
}
void debug_curl_free(char* ptr)
{
curl_free(ptr);
Dout(dc::curl, "curl_free(0x" << std::hex << (size_t)ptr << std::dec << ")");
}
time_t debug_curl_getdate(char const* datestring, time_t* now)
{
time_t ret;
ret = curl_getdate(datestring, now);
Dout(dc::curl, "curl_getdate(\"" << datestring << "\", " << (now == NULL ? "NULL" : "<erroneous non-NULL value for 'now'>") << ") = " << ret);
return ret;
}
void debug_curl_global_cleanup(void)
{
curl_global_cleanup();
Dout(dc::curl, "curl_global_cleanup()");
}
CURLcode debug_curl_global_init(long flags)
{
CURLcode ret;
ret = curl_global_init(flags);
Dout(dc::curl, "curl_global_init(0x" << std::hex << flags << std::dec << ") = " << ret);
return ret;
}
CURLMcode debug_curl_multi_add_handle(CURLM* multi_handle, CURL* easy_handle)
{
CURLMcode ret;
ret = curl_multi_add_handle(multi_handle, easy_handle);
Dout(dc::curl, "curl_multi_add_handle(" << (AICURLM*)multi_handle << ", " << (AICURL*)easy_handle << ") = " << ret);
return ret;
}
CURLMcode debug_curl_multi_assign(CURLM* multi_handle, curl_socket_t sockfd, void* sockptr)
{
CURLMcode ret;
ret = curl_multi_assign(multi_handle, sockfd, sockptr);
Dout(dc::curl, "curl_multi_assign(" << (AICURLM*)multi_handle << ", " << Socket(sockfd) << ", " << sockptr << ") = " << ret);
return ret;
}
CURLMcode debug_curl_multi_cleanup(CURLM* multi_handle)
{
CURLMcode ret;
ret = curl_multi_cleanup(multi_handle);
Dout(dc::curl, "curl_multi_cleanup(" << (AICURLM*)multi_handle << ") = " << ret);
return ret;
}
CURLMsg* debug_curl_multi_info_read(CURLM* multi_handle, int* msgs_in_queue)
{
CURLMsg* ret;
ret = curl_multi_info_read(multi_handle, msgs_in_queue);
Dout(dc::curl, "curl_multi_info_read(" << (AICURLM*)multi_handle << ", {" << *msgs_in_queue << "}) = " << ret);
return ret;
}
CURLM* debug_curl_multi_init(void)
{
CURLM* ret;
ret = curl_multi_init();
Dout(dc::curl, "curl_multi_init() = " << (AICURLM*)ret);
return ret;
}
CURLMcode debug_curl_multi_remove_handle(CURLM* multi_handle, CURL* easy_handle)
{
CURLMcode ret;
ret = curl_multi_remove_handle(multi_handle, easy_handle);
Dout(dc::curl, "curl_multi_remove_handle(" << (AICURLM*)multi_handle << ", " << (AICURL*)easy_handle << ") = " << ret);
return ret;
}
CURLMcode debug_curl_multi_setopt(CURLM* multi_handle, CURLMoption option, ...)
{
CURLMcode ret;
va_list ap;
union param_type {
long along;
void* ptr;
curl_off_t offset;
} param;
unsigned int param_type = (option / 10000) * 10000;
va_start(ap, option);
switch (param_type)
{
case CURLOPTTYPE_LONG:
param.along = va_arg(ap, long);
break;
case CURLOPTTYPE_OBJECTPOINT:
case CURLOPTTYPE_FUNCTIONPOINT:
param.ptr = va_arg(ap, void*);
break;
case CURLOPTTYPE_OFF_T:
param.offset = va_arg(ap, curl_off_t);
break;
default:
std::cerr << "Extracting param_type failed; option = " << option << "; param_type = " << param_type << std::endl;
std::exit(EXIT_FAILURE);
}
va_end(ap);
switch (param_type)
{
case CURLOPTTYPE_LONG:
ret = curl_multi_setopt(multi_handle, option, param.along);
Dout(dc::curl, "curl_easy_setopt(" << (AICURLM*)multi_handle << ", " << option << ", " << param.along << "L) = " << ret);
break;
case CURLOPTTYPE_OBJECTPOINT:
ret = curl_multi_setopt(multi_handle, option, param.ptr);
Dout(dc::curl, "curl_easy_setopt(" << (AICURLM*)multi_handle << ", " << option << ", (object*)0x" << std::hex << (size_t)param.ptr << std::dec << ") = " << ret);
break;
case CURLOPTTYPE_FUNCTIONPOINT:
ret = curl_multi_setopt(multi_handle, option, param.ptr);
Dout(dc::curl, "curl_easy_setopt(" << (AICURLM*)multi_handle << ", " << option << ", (function*)0x" << std::hex << (size_t)param.ptr << std::dec << ") = " << ret);
break;
case CURLOPTTYPE_OFF_T:
ret = curl_multi_setopt(multi_handle, option, param.offset);
Dout(dc::curl, "curl_easy_setopt(" << (AICURLM*)multi_handle << ", " << option << ", (curl_off_t)" << param.offset << ") = " << ret);
break;
default: // Stop compiler complaining about no default.
break;
}
return ret;
}
CURLMcode debug_curl_multi_socket_action(CURLM* multi_handle, curl_socket_t sockfd, int ev_bitmask, int* running_handles)
{
CURLMcode ret;
ret = curl_multi_socket_action(multi_handle, sockfd, ev_bitmask, running_handles);
Dout(dc::curl, "curl_multi_socket_action(" << (AICURLM*)multi_handle << ", " << Socket(sockfd) <<
", " << EvBitmask(ev_bitmask) << ", {" << (ret == CURLM_OK ? *running_handles : 0) << "}) = " << ret);
return ret;
}
char const* debug_curl_multi_strerror(CURLMcode errornum)
{
char const* ret;
ret = curl_multi_strerror(errornum);
Dout(dc::curl, "curl_multi_strerror(" << errornum << ") = \"" << ret << '"');
return ret;
}
struct curl_slist* debug_curl_slist_append(struct curl_slist* list, char const* string)
{
struct curl_slist* ret;
ret = curl_slist_append(list, string);
Dout(dc::curl, "curl_slist_append((curl_slist)@0x" << std::hex << (size_t)list << std::dec << ", \"" << string << "\") = " << *ret);
return ret;
}
void debug_curl_slist_free_all(struct curl_slist* list)
{
curl_slist_free_all(list);
Dout(dc::curl, "curl_slist_free_all((curl_slist)@0x" << std::hex << (size_t)list << std::dec << ")");
}
char* debug_curl_unescape(char const* url, int length)
{
char* ret;
ret = curl_unescape(url, length);
Dout(dc::curl, "curl_unescape(\"" << url << "\", " << length << ") = \"" << ret << '"');
return ret;
}
char* debug_curl_version(void)
{
char* ret;
ret = curl_version();
Dout(dc::curl, "curl_version() = \"" << ret << '"');
return ret;
}
}
#else // DEBUG_CURLIO
int debug_libcurl_dummy; // I thought some OS didn't like empty source files.
#endif // DEBUG_CURLIO

View File

@@ -1,89 +0,0 @@
#ifndef DEBUG_LIBCURL
#define DEBUG_LIBCURL
#ifndef DEBUG_CURLIO
#error "Don't include debug_libcurl.h unless DEBUG_CURLIO is defined."
#endif
#ifndef CURLINFO_TYPEMASK
#error "<curl/curl.h> must be included before including debug_libcurl.h!"
#endif
#ifndef LLPREPROCESSOR_H
// CURL_STATICLIB is needed on windows namely, which is defined in llpreprocessor.h (but only on windows).
#error "llpreprocessor.h must be included before <curl/curl.h>."
#endif
extern "C" {
extern void debug_curl_easy_cleanup(CURL* handle);
extern CURL* debug_curl_easy_duphandle(CURL* handle);
extern char* debug_curl_easy_escape(CURL* curl, char* url, int length);
extern CURLcode debug_curl_easy_getinfo(CURL* curl, CURLINFO info, ...);
extern CURL* debug_curl_easy_init(void);
extern CURLcode debug_curl_easy_pause(CURL* handle, int bitmask);
extern CURLcode debug_curl_easy_perform(CURL* handle);
extern void debug_curl_easy_reset(CURL* handle);
extern CURLcode debug_curl_easy_setopt(CURL* handle, CURLoption option, ...);
extern char const* debug_curl_easy_strerror(CURLcode errornum);
extern char* debug_curl_easy_unescape(CURL* curl, char* url, int inlength, int* outlength);
extern void debug_curl_free(char* ptr);
extern time_t debug_curl_getdate(char const* datestring, time_t* now);
extern void debug_curl_global_cleanup(void);
extern CURLcode debug_curl_global_init(long flags);
extern CURLMcode debug_curl_multi_add_handle(CURLM* multi_handle, CURL* easy_handle);
extern CURLMcode debug_curl_multi_assign(CURLM* multi_handle, curl_socket_t sockfd, void* sockptr);
extern CURLMcode debug_curl_multi_cleanup(CURLM* multi_handle);
extern CURLMsg* debug_curl_multi_info_read(CURLM* multi_handle, int* msgs_in_queue);
extern CURLM* debug_curl_multi_init(void);
extern CURLMcode debug_curl_multi_remove_handle(CURLM* multi_handle, CURL* easy_handle);
extern CURLMcode debug_curl_multi_setopt(CURLM* multi_handle, CURLMoption option, ...);
extern CURLMcode debug_curl_multi_socket_action(CURLM* multi_handle, curl_socket_t sockfd, int ev_bitmask, int* running_handles);
extern char const* debug_curl_multi_strerror(CURLMcode errornum);
extern struct curl_slist* debug_curl_slist_append(struct curl_slist* list, char const* string);
extern void debug_curl_slist_free_all(struct curl_slist* list);
extern char* debug_curl_unescape(char const* url, int length);
extern char* debug_curl_version(void);
}
#ifndef COMPILING_DEBUG_LIBCURL_CC
#ifdef curl_easy_setopt
#undef curl_easy_setopt
#undef curl_easy_getinfo
#undef curl_multi_setopt
#endif
#define curl_easy_cleanup(handle) debug_curl_easy_cleanup(handle)
#define curl_easy_duphandle(handle) debug_curl_easy_duphandle(handle)
#define curl_easy_escape(curl, url, length) debug_curl_easy_escape(curl, url, length)
#define curl_easy_getinfo(curl, info, param) debug_curl_easy_getinfo(curl, info, param)
#define curl_easy_init() debug_curl_easy_init()
#define curl_easy_pause(handle, bitmask) debug_curl_easy_pause(handle, bitmask)
#define curl_easy_perform(handle) debug_curl_easy_perform(handle)
#define curl_easy_reset(handle) debug_curl_easy_reset(handle)
#define curl_easy_setopt(handle, option, param) debug_curl_easy_setopt(handle, option, param)
#define curl_easy_strerror(errornum) debug_curl_easy_strerror(errornum)
#define curl_easy_unescape(curl, url, inlength, outlength) debug_curl_easy_unescape(curl, url, inlength, outlength)
#define curl_free(ptr) debug_curl_free(ptr)
#define curl_getdate(datestring, now) debug_curl_getdate(datestring, now)
#define curl_global_cleanup() debug_curl_global_cleanup()
#define curl_global_init(flags) debug_curl_global_init(flags)
#define curl_multi_add_handle(multi_handle, easy_handle) debug_curl_multi_add_handle(multi_handle, easy_handle)
#define curl_multi_assign(multi_handle, sockfd, sockptr) debug_curl_multi_assign(multi_handle, sockfd, sockptr)
#define curl_multi_cleanup(multi_handle) debug_curl_multi_cleanup(multi_handle)
#define curl_multi_info_read(multi_handle, msgs_in_queue) debug_curl_multi_info_read(multi_handle, msgs_in_queue)
#define curl_multi_init() debug_curl_multi_init()
#define curl_multi_remove_handle(multi_handle, easy_handle) debug_curl_multi_remove_handle(multi_handle, easy_handle)
#define curl_multi_setopt(multi_handle, option, param) debug_curl_multi_setopt(multi_handle, option, param)
#define curl_multi_socket_action(multi_handle, sockfd, ev_bitmask, running_handles) debug_curl_multi_socket_action(multi_handle, sockfd, ev_bitmask, running_handles)
#define curl_multi_strerror(errornum) debug_curl_multi_strerror(errornum)
#define curl_slist_append(list, string) debug_curl_slist_append(list, string)
#define curl_slist_free_all(list) debug_curl_slist_free_all(list)
#define curl_unescape(url, length) debug_curl_unescape(url, length)
#define curl_version() debug_curl_version()
#endif // !COMPILING_DEBUG_LIBCURL_CC
#endif // DEBUG_LIBCURL