File: plurlsrc.cpp

package info (click to toggle)
paintlib 2.6.2-14
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,920 kB
  • ctags: 3,874
  • sloc: cpp: 25,209; sh: 10,605; ansic: 1,891; makefile: 120
file content (214 lines) | stat: -rw-r--r-- 5,063 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
/--------------------------------------------------------------------
|
|      $Id: plurlsrc.cpp,v 1.6 2004/09/11 12:41:35 uzadow Exp $
|      URL Data Source Class
|
|      This is a class which takes a URL as a source of
|      picture data.
|
|      Copyright (c) 1996-2002 Ulrich von Zadow
|
\--------------------------------------------------------------------
*/

#include "plurlsrc.h"
#include "plexcept.h"

#include <stdlib.h>

using namespace std;

PLCurlInitializer PLURLSource::s_CurlInitializer;

PLURLSource::PLURLSource()
  : PLDataSource (NULL) // Progress notification not implemented.
{
  m_RawData.reserve(32786);  // Save a few reallocations.
}

PLURLSource::~PLURLSource
    ()
{
}

size_t PLURLSource::WriteCurlData(void *ptr, size_t size, size_t nmemb, void *stream)
{
  PLURLSource * This = (PLURLSource *)stream;
  This->m_RawData.append ((char*)ptr, size*nmemb);
  return size*nmemb;
}
 
int PLURLSource::Open (const char *pURL)
{
  // Easy implementation: get everything on open.
  CURL * CurlHandle = s_CurlInitializer.getHandle();

  curl_easy_setopt(CurlHandle, CURLOPT_URL, pURL);
  curl_easy_setopt(CurlHandle, CURLOPT_NOPROGRESS, 1);

  curl_easy_setopt(CurlHandle, CURLOPT_WRITEFUNCTION, WriteCurlData);
  curl_easy_setopt(CurlHandle, CURLOPT_FILE, (void*)this);  // New curl version wants CURLOPT_WRITEDATA here.
  char szErrBuf[CURL_ERROR_SIZE];
  curl_easy_setopt(CurlHandle, CURLOPT_ERRORBUFFER, szErrBuf);

  CURLcode res = curl_easy_perform(CurlHandle);
  if (res != CURLE_OK) {
    m_ErrCode = res;
    m_sErrStr = szErrBuf;
    return PL_ERRURL_SOURCE;
  } else {
    long HTTPCode;
    CURLcode res = curl_easy_getinfo(CurlHandle, CURLINFO_HTTP_CODE, &HTTPCode);
    if (HTTPCode > 399) {
        m_ErrCode = HTTPCode;
        m_sErrStr = httpErr2Str(HTTPCode);
        return PL_ERRURL_SOURCE;
    }

    m_CurPos = 0;
    PLDataSource::Open(pURL, m_RawData.length());
    return 0;
  }
}

void PLURLSource::Close
    ()
{
  m_RawData = "";
  PLDataSource::Close();
}


PLBYTE * PLURLSource::ReadNBytes
    ( int n
    )
{
  PLDataSource::ReadNBytes(n);
  
  m_CurPos += n;
  
  return (PLBYTE*)m_RawData.data()+m_CurPos-n;
}


PLBYTE * PLURLSource::ReadEverything
    ()
{
  return (PLBYTE*)(m_RawData.data()+m_CurPos);
}

void PLURLSource::Seek
    ( int n
    )
{
  m_CurPos = n;
}

// Read but don't advance file pointer.
PLBYTE * PLURLSource::GetBufferPtr
    ( int MinBytesInBuffer
    )
{
  return (PLBYTE*)(m_RawData.data()+m_CurPos);
}

const char * PLURLSource::GetCurlErrStr()
{
  return m_sErrStr.c_str();
}

int PLURLSource::GetCurlErrCode()
{
  return m_ErrCode;
}

const string PLURLSource::httpErr2Str(int HTTPCode)
{
  switch (HTTPCode) {
    case 400: return "Bad Request";
    case 401: return "Unauthorized";
    case 402: return "Payment Required";
    case 403: return "Forbidden";
    case 404: return "Not Found";
    case 405: return "Method Not Allowed";
    case 406: return "Not Acceptable";
    case 407: return "Proxy Authentication Required";
    case 408: return "Request Time-out";
    case 409: return "Conflict";
    case 410: return "Gone";
    case 411: return "Length Required";
    case 412: return "Precondition Failed";
    case 413: return "Request Entity Too Large";
    case 414: return "Request-URI Too Large";
    case 415: return "Unsupported Media Type";
    case 416: return "Requested range not satisfiable";
    case 417: return "Expectation Failed";
    case 500: return "Internal Server Error";
    case 501: return "Not Implemented";
    case 502: return "Bad Gateway";
    case 503: return "Service Unavailable";
    case 504: return "Gateway Time-out";
    case 505: return "HTTP Version not supported";
    default: {
       char sz[10];
#ifdef WIN32       
      _itoa(HTTPCode, sz, 10);
#else
      sprintf (sz, "%d", HTTPCode);
#endif      
      return string("Unknown error")+sz;
    }
  }
}

PLCurlInitializer::PLCurlInitializer () 
{
  curl_global_init(CURL_GLOBAL_ALL);
  m_CurlHandle = curl_easy_init();
  
  
}

PLCurlInitializer::~PLCurlInitializer ()
{
  finalize();
}

CURL * PLCurlInitializer::getHandle()
{
  return m_CurlHandle;
}

void PLCurlInitializer::finalize()
{
  curl_easy_cleanup(m_CurlHandle);
  curl_global_cleanup();
}


/*
/--------------------------------------------------------------------
|
|      $Log: plurlsrc.cpp,v $
|      Revision 1.6  2004/09/11 12:41:35  uzadow
|      removed plstdpch.h
|
|      Revision 1.5  2004/08/20 14:39:38  uzadow
|      Added rle format to sgi decoder. Doesn't work yet, though.
|
|      Revision 1.4  2004/04/18 12:34:45  uzadow
|      Changes for new libcurl version.
|
|      Revision 1.3  2003/04/14 10:48:19  uzadow
|      Added connection reuse capability to PLURLSource.
|
|      Revision 1.2  2003/02/24 16:19:49  uzadow
|      Added url source (linux only), improved configure support for libungif detection.
|
|      Revision 1.1  2003/02/15 21:26:58  uzadow
|      Added win32 version of url data source.
|
|
\--------------------------------------------------------------------
*/