1 29 30 package com.caucho.quercus.lib.curl; 31 32 import com.caucho.quercus.QuercusModuleException; 33 import com.caucho.quercus.env.*; 34 import com.caucho.quercus.lib.file.BinaryInput; 35 import com.caucho.quercus.lib.file.BinaryOutput; 36 import com.caucho.quercus.lib.file.FileModule; 37 import com.caucho.util.L10N; 38 import com.caucho.vfs.Path; 39 import com.caucho.vfs.WriteStream; 40 41 import java.io.IOException ; 42 import java.net.HttpURLConnection ; 43 import java.util.HashMap ; 44 import java.util.Map ; 45 import java.util.logging.Logger ; 46 47 public class CurlResource 48 { 49 private static final Logger log 50 = Logger.getLogger(CurlResource.class.getName()); 51 private static final L10N L = new L10N(CurlResource.class); 52 53 private String _requestMethod = "GET"; 54 private int _responseCode; 55 56 private String _URL; 57 private int _port = -1; 58 59 private String _username; 60 private String _password; 61 62 private boolean _isProxying = false; 63 private String _proxyUsername; 64 private String _proxyPassword; 65 private String _proxyURL; 66 private String _proxyType = "HTTP"; 67 private int _proxyPort = -1; 68 69 private boolean _isFollowingRedirects = true; 70 private boolean _isReturningBody = true; 71 private boolean _isReturningData = false; 72 private boolean _isReturningHeader = false; 73 74 private boolean _ifModifiedSince = true; 75 private String _modifiedTime; 76 77 private int _errorCode = CurlModule.CURLE_OK; 78 private String _error = ""; 79 private boolean _failOnError = false; 80 private boolean _isVerbose = false; 81 82 private int _readTimeout = -1; 83 private int _connectTimeout = -1; 84 85 private HashMap <String ,String > _requestProperties 86 = new HashMap <String , String >(); 87 88 private StringValue _header; 89 private StringValue _body; 90 private BinaryValue _postBody; 91 92 private String _contentType; 93 private int _contentLength; 94 95 private String _cookie; 96 private String _cookieFilename; 97 98 private BinaryOutput _outputFile; 99 private BinaryOutput _outputHeaderFile; 100 private BinaryInput _uploadFile; 101 private int _uploadFileSize; 102 103 private Callback _headerFunction; 104 private Callback _passwordFunction; 105 private Callback _readFunction; 106 private Callback _writeFunction; 107 108 public CurlResource() 109 { 110 } 111 112 115 public Value getBody() 116 { 117 return _body; 118 } 119 120 123 public void setBody(StringValue body) 124 { 125 _body = body; 126 } 127 128 131 public int getConnectTimeout() 132 { 133 return _connectTimeout; 134 } 135 136 139 public void setConnectTimeout(int timeout) 140 { 141 _connectTimeout = timeout; 142 } 143 144 147 public int getContentLength() 148 { 149 return _contentLength; 150 } 151 152 155 public void setContentLength(int length) 156 { 157 _contentLength = length; 158 } 159 160 163 public String getContentType() 164 { 165 return _contentType; 166 } 167 168 171 public void setContentType(String type) 172 { 173 _contentType = type; 174 } 175 176 179 public void setCookie(String cookie) 180 { 181 _cookie = cookie; 182 } 183 184 187 public void setCookieFilename(String filename) 188 { 189 _cookieFilename = filename; 190 } 191 192 195 public String getError() 196 { 197 return _error; 198 } 199 200 203 public void setError(String error) 204 { 205 _error = error; 206 } 207 208 211 public int getErrorCode() 212 { 213 return _errorCode; 214 } 215 216 219 public void setErrorCode(int code) 220 { 221 _errorCode = code; 222 } 223 224 227 public void setFailOnError(boolean failOnError) 228 { 229 _failOnError = failOnError; 230 } 231 232 235 public Value getHeader() 236 { 237 return _header; 238 } 239 240 243 public void setHeader(StringValue header) 244 { 245 _header = header; 246 } 247 248 251 public void setHeaderFunction(Callback callback) 252 { 253 _headerFunction = callback; 254 } 255 256 260 public void setIfModifiedSince(boolean option) 261 { 262 _ifModifiedSince = option; 263 } 264 265 268 public boolean getIsFollowingRedirects() 269 { 270 return _isFollowingRedirects; 271 } 272 273 276 public void setIsFollowingRedirects(boolean followRedirects) 277 { 278 _isFollowingRedirects = followRedirects; 279 } 280 281 284 public boolean getIsProxying() 285 { 286 return _isProxying; 287 } 288 289 292 public void setIsProxying(boolean proxy) 293 { 294 _isProxying = proxy; 295 } 296 297 300 public void setIsReturningBody(boolean returnBody) 301 { 302 _isReturningBody = returnBody; 303 } 304 305 308 public void setIsReturningData(boolean returnData) 309 { 310 _isReturningData = returnData; 311 } 312 313 316 public void setIsReturningHeader(boolean returnHeader) 317 { 318 _isReturningHeader = returnHeader; 319 } 320 321 324 public boolean getIsVerbose() 325 { 326 return _isVerbose; 327 } 328 329 332 public void setIsVerbose(boolean verbose) 333 { 334 _isVerbose = verbose; 335 } 336 337 340 public void setModifiedTime(String time) 341 { 342 _modifiedTime = time; 343 } 344 345 348 public void setOutputFile(BinaryOutput file) 349 { 350 _outputFile = file; 351 } 352 353 356 public void setOutputHeaderFile(BinaryOutput file) 357 { 358 _outputHeaderFile = file; 359 } 360 361 364 public String getPassword() 365 { 366 return _password; 367 } 368 369 372 public void setPassword(String pwd) 373 { 374 _password = pwd; 375 } 376 377 380 public void setPasswordFunction(Callback callback) 381 { 382 _passwordFunction = callback; 383 } 384 385 388 public int getPort() 389 { 390 return _port; 391 } 392 393 396 public void setPort(int port) 397 { 398 _port = port; 399 } 400 401 404 public BinaryValue getPostBody() 405 { 406 return _postBody; 407 } 408 409 412 public void setPostBody(BinaryValue body) 413 { 414 _postBody = body; 415 } 416 417 420 public String getProxyPassword() 421 { 422 return _proxyPassword; 423 } 424 425 428 public void setProxyPassword(String pass) 429 { 430 _proxyPassword = pass; 431 } 432 433 436 public int getProxyPort() 437 { 438 return _proxyPort; 439 } 440 441 444 public void setProxyPort(int port) 445 { 446 _proxyPort = port; 447 } 448 449 452 public String getProxyType() 453 { 454 return _proxyType; 455 } 456 457 460 public void setProxyType(String type) 461 { 462 _proxyType = type; 463 } 464 465 468 public String getProxyURL() 469 { 470 return _proxyURL; 471 } 472 473 476 public void setProxyURL(String proxy) 477 { 478 _proxyURL = proxy; 479 } 480 481 484 public String getProxyUsername() 485 { 486 return _proxyUsername; 487 } 488 489 492 public void setProxyUsername(String user) 493 { 494 _proxyUsername = user; 495 } 496 497 500 public void setReadFunction(Callback callback) 501 { 502 _readFunction = callback; 503 } 504 505 508 public int getReadTimeout() 509 { 510 return _readTimeout; 511 } 512 513 516 public void setReadTimeout(int timeout) 517 { 518 _readTimeout = timeout; 519 } 520 521 524 public String getRequestMethod() 525 { 526 return _requestMethod; 527 } 528 529 532 public void setRequestMethod(String method) 533 { 534 _requestMethod = method; 535 } 536 537 540 public HashMap <String ,String > getRequestPropertiesMap() 541 { 542 return _requestProperties; 543 } 544 545 548 public Value getRequestProperties() 549 { 550 BinaryBuilderValue bb = new BinaryBuilderValue(); 551 552 for (Map.Entry <String ,String > entry: _requestProperties.entrySet()) { 553 bb.appendBytes(entry.getKey()); 554 bb.appendBytes(": "); 555 bb.appendBytes(entry.getValue()); 556 bb.appendBytes("\r\n"); 557 } 558 559 bb.appendBytes("\r\n"); 560 561 return bb; 562 } 563 564 567 public void setRequestProperty(String key, String value) 568 { 569 _requestProperties.put(key, value); 570 } 571 572 575 public int getResponseCode() 576 { 577 return _responseCode; 578 } 579 580 583 public void setResponseCode(int code) 584 { 585 _responseCode = code; 586 } 587 588 591 public BinaryInput getUploadFile() 592 { 593 return _uploadFile; 594 } 595 596 599 public void setUploadFile(BinaryInput file) 600 { 601 _uploadFile = file; 602 } 603 604 607 public int getUploadFileSize() 608 { 609 return _uploadFileSize; 610 } 611 612 615 public void setUploadFileSize(int size) 616 { 617 _uploadFileSize = size; 618 } 619 620 623 public String getURL() 624 { 625 return _URL; 626 } 627 628 631 public void setURL(String url) 632 { 633 _URL = url; 634 } 635 636 639 public String getUsername() 640 { 641 return _username; 642 } 643 644 647 public void setUsername(String user) 648 { 649 _username = user; 650 } 651 652 655 public void setWriteFunction(Callback callback) 656 { 657 _writeFunction = callback; 658 } 659 660 663 public void removeRequestProperty(String key) 664 { 665 _requestProperties.remove(key); 666 } 667 668 669 672 private void init() 673 { 674 _error = null; 675 _errorCode = CurlModule.CURLE_OK; 676 677 if (_modifiedTime != null) { 678 if (_ifModifiedSince) { 679 removeRequestProperty("If-Unmodified-Since"); 680 setRequestProperty("If-Modified-Since", _modifiedTime); 681 } 682 else { 683 removeRequestProperty("If-Modified-Since"); 684 setRequestProperty("If-Unmodified-Since", _modifiedTime); 685 } 686 } 687 688 if (_cookie != null) 689 setRequestProperty("Cookie", _cookie); 690 else 691 removeRequestProperty("Cookie"); 692 } 693 694 697 public Value execute(Env env) 698 { 699 init(); 700 701 HttpRequest httpRequest = HttpRequest.getRequest(this); 702 703 env.addClose(httpRequest); 704 705 httpRequest.execute(env); 706 707 if (hasError()) 708 return BooleanValue.FALSE; 709 710 if (_cookie != null && _cookieFilename != null) 711 saveCookie(env); 712 713 return getReturnValue(env); 714 } 715 716 719 private Value getReturnValue(Env env) 720 { 721 StringValue data; 722 723 if (_responseCode == HttpURLConnection.HTTP_NOT_MODIFIED || 724 _responseCode == HttpURLConnection.HTTP_PRECON_FAILED || 725 (_failOnError && _responseCode >= 400)) { 726 if (_isReturningHeader) 727 data = _header; 728 else 729 return BooleanValue.TRUE; 730 } 731 else { 732 BinaryBuilderValue bb = new BinaryBuilderValue(); 733 734 if (_isReturningHeader) 735 _header.appendTo(bb); 736 737 if (_isReturningBody) 738 _body.appendTo(bb); 739 740 data = bb; 741 } 742 743 if (_isReturningData) 744 return data; 745 746 if (_outputHeaderFile != null) { 747 FileModule.fwrite(env, 748 _outputHeaderFile, 749 _header.toInputStream(), 750 Integer.MAX_VALUE); 751 } 752 753 if (_outputFile != null) { 754 FileModule.fwrite(env, 755 _outputFile, 756 data.toInputStream(), 757 Integer.MAX_VALUE); 758 759 } 760 else { 761 env.print(data); 762 } 763 764 return BooleanValue.TRUE; 765 } 766 767 770 private void saveCookie(Env env) 771 { 772 WriteStream out = null; 773 774 try { 775 Path path = env.getPwd().lookup(_cookieFilename); 776 777 out = path.openWrite(); 778 779 int len = _cookie.length(); 780 781 for (int i = 0; i < len; i++) { 782 out.write((byte)_cookie.charAt(i)); 783 } 784 } 785 catch (IOException e) { 786 throw new QuercusModuleException(e); 787 } 788 finally { 789 try { 790 if (out != null) 791 out.close(); 792 } 793 catch (IOException e) {} 794 } 795 } 796 797 800 public void close() 801 { 802 } 803 804 807 protected boolean hasError() 808 { 809 return _errorCode != CurlModule.CURLE_OK; 810 } 811 812 815 public CurlResource clone() 816 { 817 CurlResource curl = new CurlResource(); 818 819 curl.setBody(_body); 820 curl.setConnectTimeout(_connectTimeout); 821 curl.setContentLength(_contentLength); 822 curl.setContentType(_contentType); 823 curl.setCookie(_cookie); 824 curl.setCookieFilename(_cookieFilename); 825 curl.setError(_error); 826 curl.setErrorCode(_errorCode); 827 curl.setFailOnError(_failOnError); 828 curl.setHeaderFunction(_headerFunction); 829 curl.setHeader(_header); 830 curl.setIsFollowingRedirects(_isFollowingRedirects); 831 curl.setIfModifiedSince(_ifModifiedSince); 832 curl.setIsProxying(_isProxying); 833 curl.setIsReturningBody(_isReturningBody); 834 curl.setIsReturningData(_isReturningData); 835 curl.setIsReturningHeader(_isReturningHeader); 836 curl.setIsVerbose(_isVerbose); 837 curl.setModifiedTime(_modifiedTime); 838 curl.setOutputFile(_outputFile); 839 curl.setOutputHeaderFile(_outputHeaderFile); 840 curl.setPassword(_password); 841 curl.setPasswordFunction(_passwordFunction); 842 curl.setPort(_port); 843 curl.setPostBody(_postBody); 844 curl.setProxyPassword(_proxyPassword); 845 curl.setProxyPort(_proxyPort); 846 curl.setProxyType(_proxyType); 847 curl.setProxyURL(_proxyURL); 848 curl.setProxyUsername(_proxyUsername); 849 curl.setReadFunction(_readFunction); 850 curl.setReadTimeout(_readTimeout); 851 curl.setRequestMethod(_requestMethod); 852 853 for (Map.Entry <String ,String > entry: _requestProperties.entrySet()) { 854 curl.setRequestProperty(entry.getKey(), entry.getValue()); 855 } 856 857 curl.setResponseCode(_responseCode); 858 curl.setUploadFile(_uploadFile); 859 curl.setUploadFileSize(_uploadFileSize); 860 curl.setURL(_URL); 861 curl.setUsername(_username); 862 curl.setWriteFunction(_writeFunction); 863 864 return curl; 865 } 866 867 public String toString() 868 { 869 return "CurlResource[" + _requestMethod + "]"; 870 } 871 872 } 873 | Popular Tags |