KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > curl > CurlModule


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Nam Nguyen
28  */

29
30 package com.caucho.quercus.lib.curl;
31
32 import com.caucho.quercus.QuercusModuleException;
33 import com.caucho.quercus.UnimplementedException;
34 import com.caucho.quercus.annotation.NotNull;
35 import com.caucho.quercus.annotation.Optional;
36 import com.caucho.quercus.annotation.Reference;
37 import com.caucho.quercus.annotation.ReturnNullAsFalse;
38 import com.caucho.quercus.env.*;
39 import com.caucho.quercus.lib.file.BinaryInput;
40 import com.caucho.quercus.lib.file.BinaryOutput;
41 import com.caucho.quercus.module.AbstractQuercusModule;
42 import com.caucho.util.L10N;
43 import com.caucho.util.QDate;
44 import com.caucho.vfs.Path;
45 import com.caucho.vfs.ReadStream;
46
47 import java.io.IOException JavaDoc;
48 import java.util.Map JavaDoc;
49 import java.util.logging.Level JavaDoc;
50 import java.util.logging.Logger JavaDoc;
51
52 public class CurlModule
53   extends AbstractQuercusModule
54 {
55   private static final Logger JavaDoc log
56     = Logger.getLogger(CurlModule.class.getName());
57   private static final L10N L = new L10N(CurlModule.class);
58
59   public static final int CURLOPT_AUTOREFERER = 1;
60   public static final int CURLOPT_COOKIESESSION = 2;
61 // public static final int CURLOPT_DNS_USE_GLOBAL_CACHE = 3;
62
// public static final int CURLOPT_DNS_CACHE_TIMEOUT = 4;
63
// public static final int CURLOPT_FTPSSLAUTH = 5;
64
public static final int CURLOPT_PORT = 6;
65   public static final int CURLOPT_FILE = 7;
66   public static final int CURLOPT_INFILE = 8;
67   public static final int CURLOPT_INFILESIZE = 9;
68   public static final int CURLOPT_URL = 10;
69   public static final int CURLOPT_PROXY = 11;
70   public static final int CURLOPT_VERBOSE = 12;
71   public static final int CURLOPT_HEADER = 13;
72   public static final int CURLOPT_HTTPHEADER = 14;
73   public static final int CURLOPT_NOPROGRESS = 15;
74   public static final int CURLOPT_NOBODY = 16;
75   public static final int CURLOPT_FAILONERROR = 17;
76   public static final int CURLOPT_UPLOAD = 18;
77   public static final int CURLOPT_POST = 19;
78 // public static final int CURLOPT_FTPLISTONLY = 20;
79
// public static final int CURLOPT_FTPAPPEND = 21;
80
public static final int CURLOPT_NETRC = 22;
81   public static final int CURLOPT_FOLLOWLOCATION = 23;
82 // public static final int CURLOPT_FTPASCII = 24;
83
public static final int CURLOPT_PUT = 25;
84   public static final int CURLOPT_MUTE = 26;
85   public static final int CURLOPT_USERPWD = 27;
86   public static final int CURLOPT_PROXYUSERPWD = 28;
87   public static final int CURLOPT_RANGE = 29;
88   public static final int CURLOPT_TIMEOUT = 30;
89   public static final int CURLOPT_POSTFIELDS = 31;
90   public static final int CURLOPT_REFERER = 32;
91   public static final int CURLOPT_USERAGENT = 33;
92 // public static final int CURLOPT_FTPPORT = 34;
93
// public static final int CURLOPT_FTP_USE_EPSV = 35;
94
public static final int CURLOPT_LOW_SPEED_LIMIT = 36;
95   public static final int CURLOPT_LOW_SPEED_TIME = 37;
96 // public static final int CURLOPT_RESUME_FROM = 38;
97
public static final int CURLOPT_COOKIE = 39;
98 // public static final int CURLOPT_SSLCERT = 40;
99
// public static final int CURLOPT_SSLCERTPASSWD = 41;
100
public static final int CURLOPT_WRITEHEADER = 42;
101 // public static final int CURLOPT_SSL_VERIFYHOST = 43;
102
public static final int CURLOPT_COOKIEFILE = 44;
103 // public static final int CURLOPT_SSLVERSION = 45;
104
public static final int CURLOPT_TIMECONDITION = 46;
105   public static final int CURLOPT_TIMEVALUE = 47;
106   public static final int CURLOPT_CUSTOMREQUEST = 48;
107   public static final int CURLOPT_STDERR = 49;
108 // public static final int CURLOPT_TRANSFERTEXT = 50;
109
public static final int CURLOPT_RETURNTRANSFER = 51;
110 // public static final int CURLOPT_QUOTE = 52;
111
// public static final int CURLOPT_POSTQUOTE = 53;
112
// public static final int CURLOPT_INTERFACE = 54;
113
// public static final int CURLOPT_KRB4LEVEL = 55;
114
public static final int CURLOPT_HTTPPROXYTUNNEL = 56;
115 // public static final int CURLOPT_FILETIME = 57;
116
public static final int CURLOPT_WRITEFUNCTION = 58;
117   public static final int CURLOPT_READFUNCTION = 59;
118   public static final int CURLOPT_PASSWDFUNCTION = 60;
119   public static final int CURLOPT_HEADERFUNCTION = 61;
120   public static final int CURLOPT_MAXREDIRS = 62;
121   public static final int CURLOPT_MAXCONNECTS = 63;
122   public static final int CURLOPT_CLOSEPOLICY = 64;
123   public static final int CURLOPT_FRESH_CONNECT = 65;
124   public static final int CURLOPT_FORBID_REUSE = 66;
125 // public static final int CURLOPT_RANDOM_FILE = 67;
126
// public static final int CURLOPT_EGDSOCKET = 68;
127
public static final int CURLOPT_CONNECTTIMEOUT = 69;
128 // public static final int CURLOPT_SSL_VERIFYPEER = 70;
129
// public static final int CURLOPT_CAINFO = 71;
130
// public static final int CURLOPT_CAPATH = 72;
131
public static final int CURLOPT_COOKIEJAR = 73;
132 // public static final int CURLOPT_SSL_CIPHER_LIST = 74;
133
public static final int CURLOPT_BINARYTRANSFER = 75;
134   public static final int CURLOPT_NOSIGNAL = 76;
135   public static final int CURLOPT_PROXYTYPE = 77;
136   public static final int CURLOPT_BUFFERSIZE = 78;
137   public static final int CURLOPT_HTTPGET = 79;
138   public static final int CURLOPT_HTTP_VERSION = 80;
139 // public static final int CURLOPT_SSLKEY = 81;
140
// public static final int CURLOPT_SSLKEYTYPE = 82;
141
// public static final int CURLOPT_SSLKEYPASSWD = 83;
142
// public static final int CURLOPT_SSLENGINE = 84;
143
// public static final int CURLOPT_SSLENGINE_DEFAULT = 85;
144
// public static final int CURLOPT_SSLCERTTYPE = 86;
145
// public static final int CURLOPT_CRLF = 87;
146
public static final int CURLOPT_ENCODING = 88;
147   public static final int CURLOPT_PROXYPORT = 89;
148   public static final int CURLOPT_UNRESTRICTED_AUTH = 90;
149 // public static final int CURLOPT_FTP_USE_EPRT = 91;
150
public static final int CURLOPT_HTTP200ALIASES = 92;
151   public static final int CURLOPT_HTTPAUTH = 93;
152   public static final int CURLAUTH_BASIC = 1;
153   public static final int CURLAUTH_DIGEST = 2;
154   public static final int CURLAUTH_GSSNEGOTIATE = 4;
155   public static final int CURLAUTH_NTLM = 8;
156   public static final int CURLAUTH_ANY = 15;
157   public static final int CURLAUTH_ANYSAFE = 14;
158   public static final int CURLOPT_PROXYAUTH = 100;
159   public static final int CURLCLOSEPOLICY_LEAST_RECENTLY_USED = 101;
160   public static final int CURLCLOSEPOLICY_LEAST_TRAFFIC = 102;
161   public static final int CURLCLOSEPOLICY_SLOWEST = 103;
162   public static final int CURLCLOSEPOLICY_CALLBACK = 104;
163   public static final int CURLCLOSEPOLICY_OLDEST = 105;
164   public static final int CURLINFO_EFFECTIVE_URL = 106;
165   public static final int CURLINFO_HTTP_CODE = 107;
166   public static final int CURLINFO_HEADER_OUT = 108;
167   public static final int CURLINFO_HEADER_SIZE = 109;
168   public static final int CURLINFO_REQUEST_SIZE = 110;
169   public static final int CURLINFO_TOTAL_TIME = 111;
170   public static final int CURLINFO_NAMELOOKUP_TIME = 112;
171   public static final int CURLINFO_CONNECT_TIME = 113;
172   public static final int CURLINFO_PRETRANSFER_TIME = 114;
173   public static final int CURLINFO_SIZE_UPLOAD = 115;
174   public static final int CURLINFO_SIZE_DOWNLOAD = 116;
175   public static final int CURLINFO_SPEED_DOWNLOAD = 117;
176   public static final int CURLINFO_SPEED_UPLOAD = 118;
177   public static final int CURLINFO_FILETIME = 119;
178 // public static final int CURLINFO_SSL_VERIFYRESULT = 120;
179
public static final int CURLINFO_CONTENT_LENGTH_DOWNLOAD = 121;
180   public static final int CURLINFO_CONTENT_LENGTH_UPLOAD = 122;
181   public static final int CURLINFO_STARTTRANSFER_TIME = 123;
182   public static final int CURLINFO_CONTENT_TYPE = 124;
183   public static final int CURLINFO_REDIRECT_TIME = 125;
184   public static final int CURLINFO_REDIRECT_COUNT = 126;
185 // public static final int CURL_VERSION_IPV6 = 127;
186
// public static final int CURL_VERSION_KERBEROS4 = 128;
187
public static final int CURL_VERSION_SSL = 129;
188   public static final int CURL_VERSION_LIBZ = 130;
189   public static final int CURLVERSION_NOW = 131;
190   public static final int CURLE_OK = 132;
191   public static final int CURLE_UNSUPPORTED_PROTOCOL = 133;
192   public static final int CURLE_FAILED_INIT = 134;
193   public static final int CURLE_URL_MALFORMAT = 135;
194   public static final int CURLE_URL_MALFORMAT_USER = 136;
195   public static final int CURLE_COULDNT_RESOLVE_PROXY = 137;
196   public static final int CURLE_COULDNT_RESOLVE_HOST = 138;
197   public static final int CURLE_COULDNT_CONNECT = 139;
198 // public static final int CURLE_FTP_WEIRD_SERVER_REPLY = 140;
199
// public static final int CURLE_FTP_ACCESS_DENIED = 141;
200
// public static final int CURLE_FTP_USER_PASSWORD_INCORRECT = 142;
201
// public static final int CURLE_FTP_WEIRD_PASS_REPLY = 143;
202
// public static final int CURLE_FTP_WEIRD_USER_REPLY = 144;
203
// public static final int CURLE_FTP_WEIRD_PASV_REPLY = 145;
204
// public static final int CURLE_FTP_WEIRD_227_FORMAT = 146;
205
// public static final int CURLE_FTP_CANT_GET_HOST = 147;
206
// public static final int CURLE_FTP_CANT_RECONNECT = 148;
207
// public static final int CURLE_FTP_COULDNT_SET_BINARY = 149;
208
public static final int CURLE_PARTIAL_FILE = 150;
209 // public static final int CURLE_FTP_COULDNT_RETR_FILE = 151;
210
// public static final int CURLE_FTP_WRITE_ERROR = 152;
211
// public static final int CURLE_FTP_QUOTE_ERROR = 153;
212
public static final int CURLE_HTTP_NOT_FOUND = 154;
213   public static final int CURLE_WRITE_ERROR = 155;
214   public static final int CURLE_MALFORMAT_USER = 156;
215 // public static final int CURLE_FTP_COULDNT_STOR_FILE = 157;
216
public static final int CURLE_READ_ERROR = 158;
217   public static final int CURLE_OUT_OF_MEMORY = 159;
218   public static final int CURLE_OPERATION_TIMEOUTED = 160;
219 // public static final int CURLE_FTP_COULDNT_SET_ASCII = 161;
220
// public static final int CURLE_FTP_PORT_FAILED = 162;
221
// public static final int CURLE_FTP_COULDNT_USE_REST = 163;
222
// public static final int CURLE_FTP_COULDNT_GET_SIZE = 164;
223
public static final int CURLE_HTTP_RANGE_ERROR = 165;
224   public static final int CURLE_HTTP_POST_ERROR = 166;
225   public static final int CURLE_SSL_CONNECT_ERROR = 167;
226 // public static final int CURLE_FTP_BAD_DOWNLOAD_RESUME = 168;
227
public static final int CURLE_FILE_COULDNT_READ_FILE = 169;
228 // public static final int CURLE_LDAP_CANNOT_BIND = 170;
229
// public static final int CURLE_LDAP_SEARCH_FAILED = 171;
230
public static final int CURLE_LIBRARY_NOT_FOUND = 172;
231   public static final int CURLE_FUNCTION_NOT_FOUND = 173;
232   public static final int CURLE_ABORTED_BY_CALLBACK = 174;
233   public static final int CURLE_BAD_FUNCTION_ARGUMENT = 175;
234   public static final int CURLE_BAD_CALLING_ORDER = 176;
235   public static final int CURLE_HTTP_PORT_FAILED = 177;
236   public static final int CURLE_BAD_PASSWORD_ENTERED = 178;
237   public static final int CURLE_TOO_MANY_REDIRECTS = 179;
238 // public static final int CURLE_UNKNOWN_TELNET_OPTION = 180;
239
// public static final int CURLE_TELNET_OPTION_SYNTAX = 181;
240
public static final int CURLE_OBSOLETE = 182;
241 // public static final int CURLE_SSL_PEER_CERTIFICATE = 183;
242
public static final int CURLE_GOT_NOTHING = 184;
243 // public static final int CURLE_SSL_ENGINE_NOTFOUND = 185;
244
// public static final int CURLE_SSL_ENGINE_SETFAILED = 186;
245
public static final int CURLE_SEND_ERROR = 187;
246   public static final int CURLE_RECV_ERROR = 188;
247   public static final int CURLE_SHARE_IN_USE = 189;
248 // public static final int CURLE_SSL_CERTPROBLEM = 190;
249
// public static final int CURLE_SSL_CIPHER = 191;
250
// public static final int CURLE_SSL_CACERT = 192;
251
public static final int CURLE_BAD_CONTENT_ENCODING = 193;
252 // public static final int CURLE_LDAP_INVALID_URL = 194;
253
public static final int CURLE_FILESIZE_EXCEEDED = 195;
254 // public static final int CURLE_FTP_SSL_FAILED = 196;
255
// public static final int CURLFTPAUTH_DEFAULT = 197;
256

257   // Additional constants
258
public static final int CURL_TIMECOND_IFMODSINCE = 198;
259   public static final int CURL_TIMECOND_IFUNMODSINCE = 199;
260   public static final int CURL_HTTP_VERSION_NONE = 200;
261   public static final int CURL_HTTP_VERSION_1_0 = 201;
262   public static final int CURL_HTTP_VERSION_1_1 = 202;
263   public static final int CURLPROXY_HTTP = 203;
264   public static final int CURLPROXY_SOCKS5 = 204;
265
266   public String JavaDoc []getLoadedExtensions()
267   {
268     return new String JavaDoc[] { "curl" };
269   }
270
271   /**
272    * Closes this cURL object.
273    *
274    * @param env
275    * @param curl
276    */

277   public static void curl_close(Env env,
278                               @NotNull CurlResource curl)
279   {
280     if (curl == null)
281       return;
282
283     curl.close();
284   }
285
286   /**
287    * Returns a copy of this resource.
288    *
289    * @param env
290    * @param curl
291    */

292   @ReturnNullAsFalse
293   public static CurlResource curl_copy_handle(Env env,
294                               @NotNull CurlResource curl)
295   {
296     if (curl == null)
297       return null;
298
299     return curl.clone();
300   }
301
302   /**
303    * Returns the error code from the last operation.
304    *
305    * @param env
306    * @param curl
307    */

308   public static Value curl_errno(Env env,
309                               @NotNull CurlResource curl)
310   {
311     if (curl == null)
312       return BooleanValue.FALSE;
313
314     return LongValue.create(curl.getErrorCode());
315   }
316
317   /**
318    * Returns the error string from the last operation.
319    *
320    * @param env
321    * @param curl
322    */

323   public static Value curl_error(Env env,
324                               @NotNull CurlResource curl)
325   {
326     if (curl == null)
327       return BooleanValue.FALSE;
328
329     return new StringValueImpl(curl.getError());
330   }
331
332   /**
333    * @param env
334    * @param curl
335    */

336   public static Value curl_exec(Env env,
337                               @NotNull CurlResource curl)
338   {
339     if (curl == null)
340       return BooleanValue.FALSE;
341
342     return curl.execute(env);
343   }
344
345   /**
346    * Returns information about the last request.
347    *
348    * @param env
349    * @param curl
350    * @param option type of information to return
351    */

352   public static Value curl_getinfo(Env env,
353                               @NotNull CurlResource curl,
354                               @Optional Value option)
355   {
356 // if (option instanceof DefaultValue)
357
// return curl.getAllInfo();
358

359     if (curl == null)
360       return BooleanValue.FALSE;
361
362     return getInfo(env, curl, option.toInt());
363   }
364
365   private static Value getInfo(Env env,
366                               CurlResource curl,
367                               int option)
368   {
369     switch (option) {
370       case CURLINFO_EFFECTIVE_URL:
371         return new StringValueImpl(curl.getURL());
372       case CURLINFO_HTTP_CODE:
373         return LongValue.create(curl.getResponseCode());
374       case CURLINFO_FILETIME:
375         break;
376       case CURLINFO_TOTAL_TIME:
377         break;
378       case CURLINFO_NAMELOOKUP_TIME:
379         break;
380       case CURLINFO_CONNECT_TIME:
381         break;
382       case CURLINFO_PRETRANSFER_TIME:
383         break;
384       case CURLINFO_STARTTRANSFER_TIME:
385         break;
386       case CURLINFO_REDIRECT_TIME:
387         break;
388       case CURLINFO_SIZE_UPLOAD:
389         break;
390       case CURLINFO_SIZE_DOWNLOAD:
391         break;
392       case CURLINFO_SPEED_DOWNLOAD:
393         break;
394       case CURLINFO_SPEED_UPLOAD:
395         break;
396       case CURLINFO_HEADER_SIZE:
397         return LongValue.create(curl.getHeader().length());
398       case CURLINFO_HEADER_OUT:
399         return curl.getHeader();
400       case CURLINFO_REQUEST_SIZE:
401         break;
402       case CURLINFO_CONTENT_LENGTH_DOWNLOAD:
403         return LongValue.create(curl.getContentLength());
404       case CURLINFO_CONTENT_LENGTH_UPLOAD:
405         break;
406       case CURLINFO_CONTENT_TYPE:
407         String JavaDoc type = curl.getContentType();
408
409         if (type == null)
410           return NullValue.NULL;
411
412         return new StringValueImpl(type);
413       default:
414         env.warning(L.l("Unknown CURL getinfo option"));
415     }
416
417     return NullValue.NULL;
418   }
419
420   /**
421    * Returns a cURL handle.
422    *
423    * @param env
424    * @param url
425    */

426   public static CurlResource curl_init(Env env,
427                               @Optional String JavaDoc url)
428   {
429     CurlResource curl = new CurlResource();
430
431     if (url.length() > 0)
432       setURL(curl, url);
433       //curl.setURL(url);
434

435     return curl;
436   }
437
438   /**
439    * Sets the url and extracts username/password from url.
440    * Format: [protocol://]?[username:password@]?host
441    */

442   private static void setURL(CurlResource curl, String JavaDoc url)
443   {
444     int atSignIndex = url.indexOf('@');
445
446     if (atSignIndex < 0) {
447       curl.setURL(url);
448       return;
449     }
450
451     int j = url.indexOf("://");
452
453     String JavaDoc protocol;
454     if (j < 0) {
455       protocol = "http://";
456       j = 0;
457     }
458     else {
459       j += 3;
460       protocol = url.substring(0, j);
461     }
462
463     int colonIndex = url.indexOf(':', j);
464
465     if (colonIndex < 0)
466       return;
467
468     curl.setUsername(url.substring(j, colonIndex++));
469     curl.setPassword(url.substring(colonIndex, atSignIndex++));
470     curl.setURL(protocol + url.substring(atSignIndex));
471   }
472
473   /**
474    * XXX: not documented by PHP
475    *
476    * @param env
477    * @param curls
478    * @param curl
479    */

480   public static LongValue curl_multi_add_handle(Env env,
481                               Value curls,
482                               Value curl)
483   {
484     throw new UnimplementedException("curl_multi_add_handle");
485   }
486
487   /**
488    * XXX: not documented by PHP
489    *
490    * @param env
491    * @param curls
492    */

493   public static LongValue curl_multi_close(Env env,
494                               Value curls)
495   {
496     throw new UnimplementedException("curl_multi_close");
497   }
498
499   /**
500    * XXX: not documented by PHP
501    *
502    * @param env
503    * @param curls
504    * @param stillRunning
505    */

506   public static LongValue curl_multi_exec(Env env,
507                               Value curls,
508                               @Reference Value stillRunning)
509   {
510     throw new UnimplementedException("curl_multi_exec");
511   }
512
513   /**
514    * XXX: not documented by PHP
515    *
516    * @param env
517    * @param curl
518    */

519   public static StringValue curl_multi_getcontent(Env env,
520                               Value curl)
521   {
522     throw new UnimplementedException("curl_multi_getcontent");
523   }
524
525   /**
526    * XXX: not documented by PHP
527    *
528    * @param env
529    * @param curls
530    */

531   public static ArrayValue curl_multi_info_read(Env env,
532                               Value curls)
533   {
534     throw new UnimplementedException("curl_multi_info_read");
535   }
536
537   /**
538    * XXX: not documented by PHP
539    *
540    * @param env
541    */

542   public static Value curl_multi_init(Env env)
543   {
544     throw new UnimplementedException("curl_multi_init");
545   }
546
547   /**
548    * XXX: not documented by PHP
549    *
550    * @param env
551    * @param curls
552    * @param curl
553    */

554   public static LongValue curl_multi_remove_handle(Env env,
555                               Value curls,
556                               Value curl)
557   {
558     throw new UnimplementedException("curl_multi_remove_handle");
559   }
560
561   /**
562    * XXX: not documented by PHP
563    *
564    * @param env
565    * @param curls
566    * @param timeout
567    */

568   public static LongValue curl_multi_select(Env env,
569                               Value curls,
570                               @Optional Value timeout)
571   {
572     throw new UnimplementedException("curl_multi_select");
573   }
574
575   /**
576    * Sets an array of options.
577    *
578    * @param env
579    * @param curl
580    * @param options
581    */

582   public static BooleanValue curl_setopt_array(Env env,
583                               @NotNull CurlResource curl,
584                               ArrayValue options)
585   {
586     if (curl == null)
587       return BooleanValue.FALSE;
588
589     for (Map.Entry JavaDoc<Value,Value> entry: options.entrySet()) {
590       if (setOption(env, curl, entry.getKey().toInt(), entry.getValue()))
591         return BooleanValue.FALSE;
592     }
593
594     return BooleanValue.TRUE;
595   }
596
597   /**
598    * Sets a cURL option.
599    *
600    * @param env
601    * @param curl
602    * @param option
603    * @param value
604    *
605    * @return true if successful
606    */

607   public static BooleanValue curl_setopt(Env env,
608                               @NotNull CurlResource curl,
609                               int option,
610                               Value value)
611   {
612     if (curl == null)
613       return BooleanValue.FALSE;
614
615     if (setOption(env, curl, option, value))
616       return BooleanValue.TRUE;
617     else
618       return BooleanValue.FALSE;
619   }
620
621   private static boolean setOption(Env env,
622                               CurlResource curl,
623                               int option,
624                               Value value)
625   {
626     int i;
627
628     switch (option) {
629       //
630
// booleans
631
//
632
case CURLOPT_AUTOREFERER:
633         //XXX
634
break;
635       case CURLOPT_COOKIESESSION:
636         curl.setCookie(null);
637         break;
638       case CURLOPT_FAILONERROR:
639         curl.setFailOnError(value.toBoolean());
640         break;
641       case CURLOPT_FOLLOWLOCATION:
642         curl.setIsFollowingRedirects(value.toBoolean());
643         break;
644       case CURLOPT_HEADER:
645         curl.setIsReturningHeader(value.toBoolean());
646         break;
647       case CURLOPT_HTTPGET:
648         curl.setRequestMethod("GET");
649         break;
650       case CURLOPT_HTTPPROXYTUNNEL:
651         curl.setIsProxying(value.toBoolean());
652         break;
653       case CURLOPT_MUTE:
654         curl.setIsVerbose(! value.toBoolean());
655         break;
656       case CURLOPT_NETRC:
657         //username:password file
658
//XXX
659
break;
660       case CURLOPT_NOBODY:
661         curl.setIsReturningBody(false);
662         break;
663       case CURLOPT_NOPROGRESS:
664         //XXX
665
break;
666       case CURLOPT_POST:
667         curl.setRequestMethod("POST");
668         break;
669       case CURLOPT_PUT:
670         curl.setRequestMethod("PUT");
671         break;
672       case CURLOPT_RETURNTRANSFER:
673         curl.setIsReturningData(value.toBoolean());
674         break;
675       case CURLOPT_UNRESTRICTED_AUTH:
676         //XXX
677
break;
678       case CURLOPT_UPLOAD:
679         if (value.toBoolean())
680           curl.setRequestMethod("PUT");
681         break;
682       case CURLOPT_VERBOSE:
683         curl.setIsVerbose(value.toBoolean());
684         break;
685
686       //
687
// ints
688
//
689
case CURLOPT_BUFFERSIZE:
690         //XXX
691
break;
692       case CURLOPT_CONNECTTIMEOUT:
693         curl.setConnectTimeout(value.toInt() * 1000);
694         break;
695       case CURLOPT_HTTP_VERSION:
696         if (value.toInt() == CURL_HTTP_VERSION_1_0) {
697           env.stub("cURL HTTP/1.0 not specifically supported");
698         }
699         break;
700       case CURLOPT_HTTPAUTH:
701         // get authentication method from server instead
702
/*
703         int method = value.toInt();
704
705         if ((method & CURLAUTH_BASIC) == CURLAUTH_BASIC)
706           curl.setAuthenticationMethod(CURLAUTH_BASIC);
707         else if ((method & CURLAUTH_DIGEST) == CURLAUTH_DIGEST)
708           curl.setAuthenticationMethod(CURLAUTH_DIGEST);
709         else
710           env.stub("cURL Http authentication method not supported");
711 */

712         break;
713       case CURLOPT_INFILESIZE:
714         curl.setUploadFileSize(value.toInt());
715         break;
716       case CURLOPT_LOW_SPEED_LIMIT:
717         //XXX
718
break;
719       case CURLOPT_LOW_SPEED_TIME:
720         //XXX
721
break;
722       case CURLOPT_MAXCONNECTS:
723         //XXX
724
break;
725       case CURLOPT_PORT:
726         curl.setPort(value.toInt());
727         break;
728       case CURLOPT_PROXYAUTH:
729         //XXX
730
break;
731       case CURLOPT_PROXYPORT:
732         curl.setProxyPort(value.toInt());
733         break;
734       case CURLOPT_PROXYTYPE:
735         switch (value.toInt()) {
736           case CURLPROXY_HTTP:
737             curl.setProxyType("HTTP");
738             break;
739           case CURLPROXY_SOCKS5:
740             curl.setProxyType("SOCKS");
741             break;
742           default:
743             env.warning(L.l("unknown curl proxy type"));
744         }
745         break;
746       case CURLOPT_TIMECONDITION:
747         switch (value.toInt()) {
748           case CURL_TIMECOND_IFMODSINCE:
749             curl.setIfModifiedSince(true);
750             break;
751           case CURL_TIMECOND_IFUNMODSINCE:
752             curl.setIfModifiedSince(false);
753             break;
754           default:
755             env.warning(L.l("invalid CURLOPT_TIMECONDITION option"));
756         }
757         break;
758       case CURLOPT_TIMEOUT:
759         curl.setReadTimeout(value.toInt() * 1000);
760         break;
761       case CURLOPT_TIMEVALUE:
762         long time = value.toInt() * 1000L;
763         String JavaDoc format = "%a, %d %b %Y %H:%M:%S %Z";
764
765         curl.setModifiedTime(QDate.formatGMT(time, format));
766         break;
767
768       //
769
// strings
770
//
771
case CURLOPT_COOKIE:
772         curl.setCookie(value.toString());
773         break;
774       case CURLOPT_COOKIEFILE:
775         // XXX: Netscape cookie format support
776
ReadStream in = null;
777
778         try {
779           Path path = env.getPwd().lookup(value.toString());
780
781           if (path.exists()) {
782             in = path.openRead();
783
784             StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
785
786             int ch;
787             while ((ch = in.read()) >= 0) {
788               sb.append((char)ch);
789             }
790
791             curl.setCookie(sb.toString());
792           }
793         }
794         catch (IOException JavaDoc e) {
795           throw new QuercusModuleException(e);
796         }
797         finally {
798           if (in != null)
799             in.close();
800         }
801         break;
802       case CURLOPT_COOKIEJAR:
803         //XXX: Netscape cookie file format
804
curl.setCookieFilename(value.toString());
805         break;
806       case CURLOPT_CUSTOMREQUEST:
807         curl.setRequestMethod(value.toString());
808         break;
809       case CURLOPT_ENCODING:
810         String JavaDoc encoding = value.toString();
811         if (encoding.length() == 0)
812           encoding = "gzip, deflate, identity";
813         curl.setRequestProperty("Accept-Encoding", encoding);
814         break;
815       case CURLOPT_POSTFIELDS:
816         curl.setRequestMethod("POST");
817         curl.setPostBody(value.toBinaryValue(env));
818         break;
819       case CURLOPT_PROXY:
820         curl.setIsProxying(true);
821         curl.setProxyURL(value.toString());
822         break;
823       case CURLOPT_PROXYUSERPWD:
824         String JavaDoc proxyUserPwd = value.toString();
825         i = proxyUserPwd.indexOf(':');
826
827         if (i >= 0)
828           curl.setProxyUsername(proxyUserPwd.substring(0, i));
829
830         curl.setProxyPassword(proxyUserPwd.substring(i + 1));
831         break;
832       case CURLOPT_RANGE:
833         curl.setRequestProperty("Range", "bytes=" + value.toString());
834         break;
835       case CURLOPT_REFERER:
836         curl.setRequestProperty("Referer", value.toString());
837         break;
838       case CURLOPT_URL:
839         setURL(curl, value.toString());
840         //curl.setURL(value.toString());
841
break;
842       case CURLOPT_USERAGENT:
843         curl.setRequestProperty("User-Agent", value.toString());
844         break;
845       case CURLOPT_USERPWD:
846         String JavaDoc userpwd = value.toString();
847         i = userpwd.indexOf(':');
848
849         if (i >= 0)
850           curl.setUsername(userpwd.substring(0, i));
851
852         curl.setPassword(userpwd.substring(i + 1));
853         break;
854
855       //
856
// arrays
857
//
858
case CURLOPT_HTTP200ALIASES:
859         //XXX: nonstandard HTTP replies like "FOO HTTP/1.1 OK"
860
break;
861       case CURLOPT_HTTPHEADER:
862         ArrayValue array = value.toArrayValue(env);
863
864         for (Map.Entry JavaDoc<Value,Value> entry: array.entrySet()) {
865           curl.setRequestProperty(entry.getKey().toString(),
866                                      entry.getValue().toString());
867         }
868         break;
869
870       //
871
// fopen stream resources
872
//
873
case CURLOPT_FILE:
874         Object JavaDoc outputFile = value.toJavaObject();
875
876         if (outputFile instanceof BinaryOutput)
877           curl.setOutputFile((BinaryOutput)outputFile);
878         break;
879       case CURLOPT_INFILE:
880         Object JavaDoc uploadFile = value.toJavaObject();
881
882         if (uploadFile instanceof BinaryInput)
883           curl.setUploadFile((BinaryInput)uploadFile);
884         break;
885       case CURLOPT_STDERR:
886         //XXX
887
break;
888       case CURLOPT_WRITEHEADER:
889         Object JavaDoc outputHeaderFile = value.toJavaObject();
890
891         if (outputHeaderFile instanceof BinaryOutput)
892           curl.setOutputHeaderFile((BinaryOutput)outputHeaderFile);
893         break;
894
895       //
896
// callback functions
897
// - nobody really knows how to use them
898
//
899
case CURLOPT_HEADERFUNCTION:
900         curl.setHeaderFunction(env.createCallback(value));
901         if (true)
902           throw new UnimplementedException("cURL callback support");
903         break;
904       case CURLOPT_PASSWDFUNCTION:
905         curl.setPasswordFunction(env.createCallback(value));
906         if (true)
907           throw new UnimplementedException("cURL callback support");
908         break;
909       case CURLOPT_READFUNCTION:
910         curl.setReadFunction(env.createCallback(value));
911         if (true)
912           throw new UnimplementedException("cURL callback support");
913         break;
914       case CURLOPT_WRITEFUNCTION:
915         curl.setWriteFunction(env.createCallback(value));
916         if (true)
917           throw new UnimplementedException("cURL callback support");
918         break;
919
920       default:
921         env.warning(L.l("CURL option unknown or unimplemented"));
922         log.log(Level.FINE, L.l("CURL option unknown orunimplemented"));
923         return false;
924     }
925
926     return true;
927   }
928
929   /**
930    * Returns the version of this cURL implementation.
931    *
932    * @param env
933    * @param version
934    */

935   public static ArrayValue curl_version(Env env,
936                               @Optional Value version)
937   {
938     throw new UnimplementedException("curl_version");
939   }
940
941 }
942
Popular Tags