00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef _WXCURLBASE_H__INCLUDED_
00016 #define _WXCURLBASE_H__INCLUDED_
00017
00018 #ifdef WXMAKINGDLL_WXCURL
00019 #define WXDLLIMPEXP_CURL WXEXPORT
00020 #elif defined(WXUSINGDLL)
00021 #define WXDLLIMPEXP_CURL WXIMPORT
00022 #else // not making nor using DLL
00023 #define WXDLLIMPEXP_CURL
00024
00025
00026
00027 #ifndef CURL_STATICLIB
00028 #define CURL_STATICLIB
00029 #endif
00030 #endif
00031
00032 #include <wx/event.h>
00033 #include <wx/string.h>
00034 #include <wx/datetime.h>
00035 #include <wx/stream.h>
00036 #include <wx/mstream.h>
00037 #include <wx/sstream.h>
00038 #include <wx/intl.h>
00039 #include <wx/filename.h>
00040
00041
00042 #include <curl/curl.h>
00043
00044
00045
00046
00047
00049
00050 BEGIN_DECLARE_EVENT_TYPES()
00051 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_CURL, wxCURL_DOWNLOAD_EVENT, 6578)
00052 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_CURL, wxCURL_UPLOAD_EVENT, 6579)
00053 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_CURL, wxCURL_BEGIN_PERFORM_EVENT, 6580)
00054 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_CURL, wxCURL_END_PERFORM_EVENT, 6581)
00055 END_DECLARE_EVENT_TYPES()
00056
00057
00058 class WXDLLIMPEXP_CURL wxCurlBase;
00059
00060
00062 class WXDLLIMPEXP_CURL wxCurlProgressBaseEvent : public wxEvent
00063 {
00064 public:
00065 wxCurlProgressBaseEvent(int id, wxEventType type,
00066 wxCurlBase *p = NULL, const wxString &url = wxEmptyString)
00067 : wxEvent(id, type) { m_pCURL = p; m_szURL = url; m_dt = wxDateTime::Now(); }
00068
00069
00070 public:
00071
00073 wxCurlBase *GetCurlSession() const { return m_pCURL; }
00074
00076 wxDateTime GetDateTime() const { return m_dt; }
00077
00079 double GetPercent() const
00080 { return GetTotalBytes() == 0 ? 0 : (100.0 * (GetTransferredBytes()/GetTotalBytes())); }
00081
00083 virtual double GetSpeed() const
00084 { return GetTransferredBytes()/GetElapsedTime().GetSeconds().ToDouble(); }
00085
00086
00087 public:
00088
00091 virtual wxTimeSpan GetElapsedTime() const;
00092
00094 virtual wxTimeSpan GetEstimatedTime() const;
00095
00097 virtual wxTimeSpan GetEstimatedRemainingTime() const;
00098
00099
00100 public:
00101
00103 wxString GetURL() const { return m_szURL; }
00104
00106 wxString GetHumanReadableSpeed(const wxString &inv = _("Not available"), int prec = 1) const;
00107
00109 wxString GetHumanReadableTotalBytes(const wxString &inv = _("Not available"), int prec = 1) const
00110 { return wxFileName::GetHumanReadableSize(wxULongLong((unsigned long)GetTotalBytes()), inv, prec); }
00111
00113 wxString GetHumanReadableTransferredBytes(const wxString &inv = _("Not available"), int prec = 1) const
00114 { return wxFileName::GetHumanReadableSize(wxULongLong((unsigned long)GetTransferredBytes()), inv, prec); }
00115
00116
00117 public:
00118
00120 virtual double GetTotalBytes() const = 0;
00121
00123 virtual double GetTransferredBytes() const = 0;
00124
00125 protected:
00126 wxCurlBase *m_pCURL;
00127 wxString m_szURL;
00128
00129
00130
00131
00132 wxDateTime m_dt;
00133 };
00134
00135
00136
00137
00139
00145 class WXDLLIMPEXP_CURL wxCurlDownloadEvent : public wxCurlProgressBaseEvent
00146 {
00147 public:
00148 wxCurlDownloadEvent();
00149 wxCurlDownloadEvent(int id, wxCurlBase *originator,
00150 const double& rDownloadTotal, const double& rDownloadNow,
00151 const wxString& szURL = wxEmptyString);
00152 wxCurlDownloadEvent(const wxCurlDownloadEvent& event);
00153
00154 virtual wxEvent* Clone() const { return new wxCurlDownloadEvent(*this); }
00155
00156
00158 double GetDownloadedBytes() const { return m_rDownloadNow; }
00159 double GetTransferredBytes() const { return m_rDownloadNow; }
00160
00162 double GetTotalBytes() const { return m_rDownloadTotal; }
00163
00165 wxString GetHumanReadableDownloadedBytes(const wxString &inv = _("Not available"), int prec = 1) const
00166 { return wxFileName::GetHumanReadableSize(wxULongLong((unsigned long)m_rDownloadNow), inv, prec); }
00167
00168
00169 protected:
00170 double m_rDownloadTotal, m_rDownloadNow;
00171
00172 private:
00173 DECLARE_DYNAMIC_CLASS(wxCurlDownloadEvent);
00174 };
00175
00176 typedef void (wxEvtHandler::*wxCurlDownloadEventFunction)(wxCurlDownloadEvent&);
00177
00178 #define wxCurlDownloadEventHandler(func) \
00179 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxCurlDownloadEventFunction, &func)
00180
00181 #define EVT_CURL_DOWNLOAD(id, fn) \
00182 wx__DECLARE_EVT1(wxCURL_DOWNLOAD_EVENT, id, wxCurlDownloadEventHandler(fn))
00183
00184
00190 class WXDLLIMPEXP_CURL wxCurlUploadEvent : public wxCurlProgressBaseEvent
00191 {
00192 public:
00193 wxCurlUploadEvent();
00194 wxCurlUploadEvent(int id, wxCurlBase *originator,
00195 const double& rUploadTotal, const double& rUploadNow,
00196 const wxString& szURL = wxEmptyString);
00197 wxCurlUploadEvent(const wxCurlUploadEvent& event);
00198
00199 virtual wxEvent* Clone() const { return new wxCurlUploadEvent(*this); }
00200
00201
00203 double GetUploadedBytes() const { return m_rUploadNow; }
00204 double GetTransferredBytes() const { return m_rUploadNow; }
00205
00207 double GetTotalBytes() const { return m_rUploadTotal; }
00208
00210 wxString GetHumanReadableUploadedBytes(const wxString &inv = _("Not available"), int prec = 1) const
00211 { return wxFileName::GetHumanReadableSize(wxULongLong((unsigned long)m_rUploadNow), inv, prec); }
00212
00213 protected:
00214 double m_rUploadTotal, m_rUploadNow;
00215
00216 private:
00217 DECLARE_DYNAMIC_CLASS(wxCurlUploadEvent);
00218 };
00219
00220 typedef void (wxEvtHandler::*wxCurlUploadEventFunction)(wxCurlUploadEvent&);
00221
00222 #define wxCurlUploadEventHandler(func) \
00223 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxCurlUploadEventFunction, &func)
00224
00225 #define EVT_CURL_UPLOAD(id, fn) \
00226 wx__DECLARE_EVT1(wxCURL_UPLOAD_EVENT, id, wxCurlUploadEventHandler(fn))
00227
00228
00229
00230
00231
00233
00236 class WXDLLIMPEXP_CURL wxCurlBeginPerformEvent : public wxEvent
00237 {
00238 public:
00239 wxCurlBeginPerformEvent();
00240 wxCurlBeginPerformEvent(int id, const wxString& szURL);
00241 wxCurlBeginPerformEvent(const wxCurlBeginPerformEvent& event);
00242
00243 virtual wxEvent* Clone() const { return new wxCurlBeginPerformEvent(*this); }
00244
00246 wxString GetURL() const { return m_szURL; }
00247
00248 protected:
00249 wxString m_szURL;
00250
00251 private:
00252 DECLARE_DYNAMIC_CLASS(wxCurlBeginPerformEvent);
00253 };
00254
00255 typedef void (wxEvtHandler::*wxCurlBeginPerformEventFunction)(wxCurlBeginPerformEvent&);
00256
00257 #define wxCurlBeginPerformEventHandler(func) \
00258 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxCurlBeginPerformEventFunction, &func)
00259
00260 #define EVT_CURL_BEGIN_PERFORM(id, fn) \
00261 wx__DECLARE_EVT1(wxCURL_BEGIN_PERFORM_EVENT, id, wxCurlBeginPerformEventHandler(fn))
00262
00263
00264
00265
00266
00268
00271 class WXDLLIMPEXP_CURL wxCurlEndPerformEvent : public wxEvent
00272 {
00273 public:
00274 wxCurlEndPerformEvent();
00275 wxCurlEndPerformEvent(int id, const wxString& szURL, const long& iResponseCode);
00276 wxCurlEndPerformEvent(const wxCurlEndPerformEvent& event);
00277
00278 virtual wxEvent* Clone() const { return new wxCurlEndPerformEvent(*this); }
00279
00281 wxString GetURL() const { return m_szURL; }
00282
00284 long GetResponseCode() const { return m_iResponseCode; }
00285
00287 bool IsSuccessful() const { return ((m_iResponseCode > 199) && (m_iResponseCode < 300)); }
00288
00289 protected:
00290 wxString m_szURL;
00291 long m_iResponseCode;
00292
00293 private:
00294 DECLARE_DYNAMIC_CLASS(wxCurlEndPerformEvent);
00295 };
00296
00297 typedef void (wxEvtHandler::*wxCurlEndPerformEventFunction)(wxCurlEndPerformEvent&);
00298
00299 #define wxCurlEndPerformEventHandler(func) \
00300 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxCurlEndPerformEventFunction, &func)
00301
00302 #define EVT_CURL_END_PERFORM(id, fn) \
00303 wx__DECLARE_EVT1(wxCURL_END_PERFORM_EVENT, id, wxCurlEndPerformEventHandler(fn))
00304
00305
00306
00307
00309
00310 extern "C"
00311 {
00312 int wxcurl_evt_progress_func(void* ptr, double rDlTotal, double rDlNow,
00313 double rUlTotal, double rUlNow);
00314 int wxcurl_verbose_stream_write (CURL * crlptr , curl_infotype info, char * cStrMessage,
00315 size_t msgSize, void * buffer);
00316 size_t wxcurl_header_func(void *ptr, size_t size, size_t nmemb, void *stream);
00317
00318 size_t wxcurl_string_write(void* ptr, size_t size, size_t nmemb, void* stream);
00319 size_t wxcurl_stream_write(void* ptr, size_t size, size_t nmemb, void* stream);
00320
00321 size_t wxcurl_string_read(void* ptr, size_t size, size_t nmemb, void* stream);
00322 size_t wxcurl_stream_read(void* ptr, size_t size, size_t nmemb, void* stream);
00323 }
00324
00325
00326
00328
00330 #define wxCURL_SEND_PROGRESS_EVENTS 0x01
00331
00333 #define wxCURL_SEND_BEGINEND_EVENTS 0x02
00334
00336 #define wxCURL_DEFAULT_FLAGS (0)
00337
00348 class WXDLLIMPEXP_CURL wxCurlBase
00349 {
00350 public:
00351 wxCurlBase(const wxString& szURL = wxEmptyString,
00352 const wxString& szUserName = wxEmptyString,
00353 const wxString& szPassword = wxEmptyString,
00354 wxEvtHandler* pEvtHandler = NULL, int id = wxID_ANY,
00355 long flags = wxCURL_DEFAULT_FLAGS);
00356
00357 virtual ~wxCurlBase();
00358
00359
00360
00361
00364 bool SetOpt(CURLoption option, ...);
00365
00368 bool GetInfo(CURLINFO info, ...) const;
00369
00376 bool Perform();
00377
00378
00379
00380
00383 bool InitHandle();
00384
00388 bool CleanupHandle();
00389
00391 bool ReInitHandle();
00392
00394 bool ResetHandle();
00395
00397 bool IsOk() const { return m_pCURL != NULL; }
00398
00399
00400
00401
00404 bool SetEvtHandler(wxEvtHandler* pParent, int id = wxID_ANY);
00405 wxEvtHandler* GetEvtHandler() const;
00406 int GetId() const;
00407
00412 void SetFlags(long flags);
00413 long GetFlags() const;
00414
00417 void SetBaseURL(const wxString& szBaseURL);
00418 wxString GetBaseURL() const;
00419
00421 void SetURL(const wxString &szRelativeURL);
00422
00424 wxString GetURL() const;
00425
00429 void SetPort(const long& iPort);
00430 long GetPort() const;
00431
00434 void SetUsername(const wxString& szUsername);
00435 wxString GetUsername() const;
00436
00439 void SetPassword(const wxString& szPassword);
00440 wxString GetPassword() const;
00441
00443 wxString GetResponseHeader() const;
00444 wxString GetResponseBody() const;
00445 long GetResponseCode() const;
00446
00448 void UseProxy(const bool& bUseProxy);
00449 bool UseProxy() const;
00450
00452 void SetProxyHost(const wxString& szProxyHost);
00453 wxString GetProxyHost() const;
00454
00456 void SetProxyUsername(const wxString& szProxyUsername);
00457 wxString GetProxyUsername() const;
00458
00460 void SetProxyPassword(const wxString& szProxyPassword);
00461 wxString GetProxyPassword() const;
00462
00464 void SetProxyPort(const long& iProxyPort);
00465 long GetProxyPort() const;
00466
00470 void SetVerbose(const bool& bVerbose);
00471 bool IsVerbose() const;
00472
00474 bool GetVerboseStream(wxOutputStream& destStream) const;
00475
00477 bool GetVerboseString(wxString& szStream) const;
00478
00480 wxString GetErrorString() const;
00481
00485 wxString GetDetailedErrorString() const;
00486
00487
00488
00489
00491 static void Init();
00492
00494 static void Shutdown();
00495
00496
00497
00498
00499 static wxDateTime GetDateFromString(const wxString& szDate);
00500 static wxString GetURLEncodedString(const wxString& szData);
00501 static wxString GetStringFromURLEncoded(const wxString& szData);
00502
00503 static wxString GetCURLVersion();
00504
00505
00506 protected:
00507
00508 friend class wxCurlBaseThread;
00509 friend class wxCurlSizeQueryThread;
00510 friend class wxCurlProgressBaseEvent;
00511
00517 void OverrideProgressCallback(curl_progress_callback newcallback, void *data)
00518 { m_progressCallback=newcallback; m_progressData=data; }
00519
00521 wxDateTime GetBeginTransferSpan() const
00522 { return m_dtBeginTransferSpan; }
00523
00526 wxTimeSpan GetElapsedTimeOffset() const
00527 { return m_tsElapsedOffset; }
00528
00530 void EndTransferSpan()
00531 {
00532 wxDateTime now = wxDateTime::Now();
00533 m_tsElapsedOffset += now - m_dtBeginTransferSpan;
00534 }
00535
00537 void BeginTransferSpan()
00538 {
00539 m_dtBeginTransferSpan = wxDateTime::Now();
00540 }
00541
00542 protected:
00543
00544
00545 CURL* m_pCURL;
00546
00547
00548
00549
00550 #define wxCURL_BUF2STRING(x) wxString((const char*)(x), wxConvLibc)
00551
00552 #if wxUSE_UNICODE
00553 #define wxCURL_STRING2BUF(x) ((x).ToAscii())
00554 #else
00555 #define wxCURL_STRING2BUF(x) wxCharBuffer((x).c_str())
00556 #endif
00557
00558 #define wxCURL_BUF_ISEMPTY(x) (strlen(x) == 0)
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574 wxCharBuffer m_szBaseURL;
00575 wxCharBuffer m_szCurrFullURL;
00576 wxCharBuffer m_szUsername;
00577 wxCharBuffer m_szPassword;
00578 wxCharBuffer m_szUserPass;
00579
00580 long m_iHostPort;
00581
00582
00583
00584 wxCharBuffer m_szResponseHeader;
00585 wxCharBuffer m_szResponseBody;
00586 long m_iResponseCode;
00587
00588
00589
00590 wxArrayString m_arrHeaders;
00591 struct curl_slist* m_pHeaders;
00592
00593
00594
00595 bool m_bUseProxy;
00596 wxCharBuffer m_szProxyHost;
00597 wxCharBuffer m_szProxyUsername;
00598 wxCharBuffer m_szProxyPassword;
00599 wxCharBuffer m_szProxyUserPass;
00600 long m_iProxyPort;
00601
00602
00603
00604 bool m_bVerbose;
00605 wxStringOutputStream m_mosVerbose;
00606
00607 char m_szDetailedErrorBuffer[CURL_ERROR_SIZE];
00608 wxCharBuffer m_szLastError;
00609
00610
00611 wxEvtHandler* m_pEvtHandler;
00612 int m_nId;
00613 long m_nFlags;
00614
00615
00616 curl_progress_callback m_progressCallback;
00617 void* m_progressData;
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636 wxDateTime m_dtBeginTransferSpan;
00637 wxTimeSpan m_tsElapsedOffset;
00638
00639
00640 protected:
00641
00642
00643 virtual void SetCurlHandleToDefaults(const wxString& relativeURL);
00644 virtual void SetHeaders();
00645 virtual void ResetHeaders();
00646 virtual void ResetResponseVars();
00647
00648
00649 void DumpErrorIfNeed(CURLcode error) const;
00650
00651 protected:
00652
00653
00654 bool SetStringOpt(CURLoption option, const wxCharBuffer &str);
00655
00656
00657 bool SetStringWriteFunction(const wxCharBuffer& str)
00658 {
00659 bool ret = true;
00660 ret &= SetOpt(CURLOPT_WRITEFUNCTION, wxcurl_string_write);
00661 ret &= SetOpt(CURLOPT_WRITEDATA, (void*)&str);
00662 return ret;
00663 }
00664
00665 bool SetStreamWriteFunction(const wxOutputStream& buf)
00666 {
00667 bool ret = true;
00668 ret &= SetOpt(CURLOPT_WRITEFUNCTION, wxcurl_stream_write);
00669 ret &= SetOpt(CURLOPT_WRITEDATA, (void*)&buf);
00670 return ret;
00671 }
00672
00673 bool SetStringReadFunction(const wxCharBuffer& str)
00674 {
00675 bool ret = true;
00676 ret &= SetOpt(CURLOPT_READFUNCTION, wxcurl_string_read);
00677 ret &= SetOpt(CURLOPT_READDATA, (void*)&str);
00678 return ret;
00679 }
00680
00681 bool SetStreamReadFunction(const wxInputStream& buf)
00682 {
00683 bool ret = true;
00684 ret &= SetOpt(CURLOPT_READFUNCTION, wxcurl_stream_read);
00685 ret &= SetOpt(CURLOPT_READDATA, (void*)&buf);
00686 return ret;
00687 }
00688 };
00689
00690 #endif // _WXCURLBASE_H__INCLUDED_