/[resiprocate]/main/sip/resiprocate/dum/test/curlHttp/CurlHttpProvider.cxx
ViewVC logotype

Contents of /main/sip/resiprocate/dum/test/curlHttp/CurlHttpProvider.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4602 - (show annotations) (download)
Thu May 12 01:27:52 2005 UTC (14 years, 7 months ago) by derek
File size: 5499 byte(s)
added missing vocal licences
1 #include "CurlHttpProvider.hxx"
2 #include "resiprocate/external/HttpGetMessage.hxx"
3 #include "resiprocate/TransactionUser.hxx"
4 #include "resiprocate/os/Logger.hxx"
5
6 #include <curl/curl.h>
7
8 #define RESIPROCATE_SUBSYSTEM Subsystem::TRANSPORT
9
10 using namespace resip;
11
12 CurlHttpProvider::CurlHttpProvider()
13 {
14 curl_global_init(CURL_GLOBAL_DEFAULT);
15 }
16
17 CurlHttpProvider::~CurlHttpProvider()
18 {
19 }
20
21 void
22 CurlHttpProvider::get(const GenericUri& target, const Data& tid, TransactionUser& tu)
23 {
24 //fix cleanup logic, self deletes for now
25 RequestThread* rt = new RequestThread(target, tid, tu);
26 rt->run();
27 }
28
29 CurlHttpProvider*
30 CurlHttpProviderFactory::createHttpProvider()
31 {
32 return new CurlHttpProvider();
33 }
34
35 CurlHttpProvider::RequestThread::RequestThread(const GenericUri& target,
36 const Data& tid,
37 TransactionUser& tu) :
38 mTarget(target),
39 mTid(tid),
40 mTransactionUser(tu),
41 mX509Blob(),
42 mStream(mX509Blob)
43 {}
44
45 CurlHttpProvider::RequestThread::~RequestThread()
46 {
47 }
48
49 void
50 CurlHttpProvider::RequestThread::thread()
51 {
52 CURL *curl = curl_easy_init();
53 if (!curl)
54 {
55 HttpGetMessage* res = new HttpGetMessage(mTid, false, Data::Empty, Mime());
56 mTransactionUser.post(res);
57 }
58
59 curl_easy_setopt(curl, CURLOPT_URL, mTarget.uri().c_str());
60 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, RequestThread::curlCallback);
61 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &mStream);
62 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
63
64 int code = 404;
65 curl_easy_perform(curl);
66 #if ( LIBCURL_VERSION_NUM > 0x070a07 )
67 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
68 #else
69 curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &code);
70 #endif
71 InfoLog ( << "Curl returned: " << code);
72
73 if (code / 100 == 2)
74 {
75 //.dcm. vodoo to trick the lazy parsers, should add convenience methods to
76 //clean this up
77 Mime contentType;
78 {
79 char* contentTypeString;
80 curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &contentTypeString);
81
82 InfoLog ( << "Content type from curl: " << contentTypeString);
83
84 if (contentTypeString)
85 {
86 HeaderFieldValue tmp(contentTypeString, strlen(contentTypeString));
87 Mime tempMime(&tmp, Headers::ContentType);
88 contentType = tempMime;
89 delete contentTypeString;
90 }
91 }
92 mStream.flush();
93 HttpGetMessage* res = new HttpGetMessage(mTid, true, mX509Blob, contentType);
94 mTransactionUser.post(res);
95 }
96 else
97 {
98 HttpGetMessage* res = new HttpGetMessage(mTid, false, Data::Empty, Mime());
99 mTransactionUser.post(res);
100 }
101 curl_easy_cleanup(curl);
102
103 delete this;
104 }
105
106 int
107 CurlHttpProvider::RequestThread::curlCallback(void *ptr, size_t size, size_t nmemb, void *stream)
108 {
109 DataStream* ds = reinterpret_cast<DataStream*>(stream);
110 assert(size == 1);
111 ds->write(reinterpret_cast<const char*>(ptr), nmemb);
112 InfoLog ( << "Wrote: " << nmemb << " to stream" );
113 return nmemb;
114 }
115
116
117 /* ====================================================================
118 * The Vovida Software License, Version 1.0
119 *
120 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
121 *
122 * Redistribution and use in source and binary forms, with or without
123 * modification, are permitted provided that the following conditions
124 * are met:
125 *
126 * 1. Redistributions of source code must retain the above copyright
127 * notice, this list of conditions and the following disclaimer.
128 *
129 * 2. Redistributions in binary form must reproduce the above copyright
130 * notice, this list of conditions and the following disclaimer in
131 * the documentation and/or other materials provided with the
132 * distribution.
133 *
134 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
135 * and "Vovida Open Communication Application Library (VOCAL)" must
136 * not be used to endorse or promote products derived from this
137 * software without prior written permission. For written
138 * permission, please contact vocal@vovida.org.
139 *
140 * 4. Products derived from this software may not be called "VOCAL", nor
141 * may "VOCAL" appear in their name, without prior written
142 * permission of Vovida Networks, Inc.
143 *
144 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
145 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
146 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
147 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
148 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
149 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
150 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
151 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
152 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
153 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
154 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
155 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
156 * DAMAGE.
157 *
158 * ====================================================================
159 *
160 * This software consists of voluntary contributions made by Vovida
161 * Networks, Inc. and many individuals on behalf of Vovida Networks,
162 * Inc. For more information on Vovida Networks, Inc., please see
163 * <http://www.vovida.org/>.
164 *
165 */

Properties

Name Value
svn:eol-style LF

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27