Advertisement
Guest User

Untitled

a guest
May 7th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. ;;; curl.lsp -- use libcurl
  2.  
  3. ;; libcurl - API
  4. ;; - http://curl.haxx.se/libcurl/c/
  5. ;; libcurl - source code examples
  6. ;; - http://curl.haxx.se/libcurl/c/example.html
  7. ;;
  8. ;; 10 awesome things to do with cURL | CatsWhoCode.com
  9. ;; - http://www.catswhocode.com/blog/10-awesome-things-to-do-with-curl
  10.  
  11. (case ostype
  12. ("Win32"
  13. ; (env "PATH" (append "C:\\tmp\\curl-7.21.7-devel-mingw32\\bin;" (env "PATH")))
  14. (define libcurl "libcurl.dll"))
  15. ("BSD"
  16. (define libcurl "libcurl.so"))
  17. (true
  18. (define libcurl "libcurl.so.3"))
  19. )
  20.  
  21. #include <curl/curl.h>
  22. (import libcurl "curl_strequal")
  23. (import libcurl "curl_strnequal")
  24. (import libcurl "curl_formadd")
  25. (import libcurl "curl_formget")
  26. (import libcurl "curl_formfree")
  27. (import libcurl "curl_getenv")
  28. (import libcurl "curl_version") ; char *curl_version();
  29. (import libcurl "curl_easy_escape") ; char *curl_easy_escape(CURL *curl, char *url, int length);
  30. (import libcurl "curl_escape") ; XXX (deprecated, do not use)
  31. (import libcurl "curl_easy_unescape") ; char *curl_easy_unescape(CURL *curl, char *url, int inlength, int * outlength);
  32. (import libcurl "curl_unescape")
  33. (import libcurl "curl_free") ; void curl_free(char *ptr);
  34. (import libcurl "curl_global_init")
  35. (import libcurl "curl_global_init_mem")
  36. (import libcurl "curl_global_cleanup")
  37. (import libcurl "curl_slist_append")
  38. (import libcurl "curl_slist_free_all")
  39. (import libcurl "curl_getdate")
  40. (import libcurl "curl_share_init")
  41. (import libcurl "curl_share_setopt")
  42. (import libcurl "curl_share_cleanup")
  43. (import libcurl "curl_version_info")
  44. (import libcurl "curl_easy_strerror") ; const char *curl_easy_strerror(CURLcode errornum);
  45. (import libcurl "curl_share_strerror")
  46. (import libcurl "curl_easy_pause")
  47.  
  48. #include <curl/easy.h>
  49. (import libcurl "curl_easy_init") ; CURL *curl_easy_init();
  50. (import libcurl "curl_easy_setopt") ; CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter);
  51. (import libcurl "curl_easy_perform") ; CURLcode curl_easy_perform(CURL *handle);
  52. (import libcurl "curl_easy_cleanup") ; void curl_easy_cleanup(CURL * handle);
  53. (import libcurl "curl_easy_getinfo") ; CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...);
  54. (import libcurl "curl_easy_duphandle")
  55. (import libcurl "curl_easy_reset")
  56. (import libcurl "curl_easy_recv")
  57. (import libcurl "curl_easy_send")
  58.  
  59. #include <curl/mprintf.h>
  60. (import libcurl "curl_mprintf")
  61. (import libcurl "curl_mfprintf")
  62. (import libcurl "curl_msprintf")
  63. (import libcurl "curl_msnprintf")
  64. (import libcurl "curl_mvprintf")
  65. (import libcurl "curl_mvfprintf")
  66. (import libcurl "curl_mvsprintf")
  67. (import libcurl "curl_mvsnprintf")
  68. (import libcurl "curl_maprintf")
  69. (import libcurl "curl_mvaprintf")
  70.  
  71. #include <curl/multi.h>
  72. (import libcurl "curl_multi_init")
  73. (import libcurl "curl_multi_add_handle")
  74. (import libcurl "curl_multi_remove_handle")
  75. (import libcurl "curl_multi_fdset")
  76. (import libcurl "curl_multi_perform")
  77. (import libcurl "curl_multi_cleanup")
  78. (import libcurl "curl_multi_info_read")
  79. (import libcurl "curl_multi_strerror")
  80. (import libcurl "curl_multi_socket")
  81. (import libcurl "curl_multi_socket_action")
  82. (import libcurl "curl_multi_socket_all")
  83. (import libcurl "curl_multi_timeout")
  84. (import libcurl "curl_multi_setopt")
  85. (import libcurl "curl_multi_assign")
  86.  
  87.  
  88. #include <curl/curl.h>
  89. (define CURL_MAX_WRITE_SIZE 16384)
  90.  
  91. ;; typedef enum { ... } CURLcode;
  92. (define CURLE_OK 0)
  93. (define CURLE_UNSUPPORTED_PROTOCOL 1)
  94. (define CURLE_WRITE_ERROR 23)
  95.  
  96. (define CURL_ERROR_SIZE 256)
  97.  
  98. #define CURLOPTTYPE_LONG 0
  99. #define CURLOPTTYPE_OBJECTPOINT 10000
  100. #define CURLOPTTYPE_FUNCTIONPOINT 20000
  101. #define CURLOPTTYPE_OFF_T 30000
  102.  
  103. ;; typedef enum { ... } CURLoption;
  104. (define CURLOPT_FILE (+ 10000 1))
  105. (define CURLOPT_URL (+ 10000 2))
  106. (define CURLOPT_PORT 3)
  107. (define CURLOPT_INFILE (+ 10000 9))
  108. (define CURLOPT_ERRORBUFFER (+ 10000 10))
  109. (define CURLOPT_WRITEFUNCTION (+ 20000 11))
  110. (define CURLOPT_POSTFIELDS (+ 10000 15))
  111. (define CURLOPT_USERAGENT (+ 10000 18))
  112. (define CURLOPT_HTTPHEADER (+ 10000 23))
  113. (define CURLOPT_WRITEHEADER (+ 10000 29))
  114.  
  115. (define CURLOPT_VERBOSE 41)
  116. (define CURLOPT_HEADER 42)
  117. (define CURLOPT_NOPROGRESS 43)
  118. (define CURLOPT_NOBODY 43)
  119. (define CURLOPT_POST 47)
  120.  
  121. (define CURLOPT_FOLLOWLOCATION 52)
  122. (define CURLOPT_SSL_VERIFYPEER 64)
  123. (define CURLOPT_SSL_VERIFYHOST 81)
  124.  
  125. (define SSL_VERIFYHOST 81)
  126.  
  127. (define CURLOPT_WRITEDATA CURLOPT_FILE)
  128. (define CURLOPT_READDATA CURLOPT_INFILE)
  129. (define CURLOPT_HEADERDATA CURLOPT_WRITEHEADER)
  130.  
  131. (define CURL_GLOBAL_SSL (<< 1 0))
  132. (define CURL_GLOBAL_WIN32 (<< 1 1))
  133. (define CURL_GLOBAL_ALL (| CURL_GLOBAL_SSL CURL_GLOBAL_WIN32))
  134. (define CURL_GLOBAL_NOTHING 0)
  135. (define CURL_GLOBAL_DEFAULT CURL_GLOBAL_ALL)
  136.  
  137.  
  138. (define (curl-version)
  139. (get-string (curl_version)))
  140.  
  141. (define (curl-easy-escape url (len 0))
  142. (letn ((curl (curl_easy_init))
  143. (res (curl_easy_escape curl url len))
  144. (str (get-string res)))
  145. (curl_free res)
  146. (curl_easy_cleanup curl)
  147. str))
  148. ;; (curl-easy-escape "newlisp.org/?q=index.html#123")
  149. ;; => "newlisp.org%2F%3Fq%3Dindex.html%23123"
  150.  
  151. (define (curl-easy-unescape url (len 0))
  152. (letn ((curl (curl_easy_init))
  153. (outlen (pack "lu" 0))
  154. (res (curl_easy_unescape curl url len outlen))
  155. (str (first (unpack (format "s%d" (get-int outlen)) res))))
  156. (curl_free res)
  157. (curl_easy_cleanup curl)
  158. str))
  159.  
  160. (define (curl-simple url)
  161. (local (curl res)
  162. (setq curl (curl_easy_init))
  163. (when (!= curl 0)
  164. (curl_easy_setopt curl CURLOPT_URL url)
  165. ;(curl_easy_setopt curl CURLOPT_HEADER 1)
  166. (setf res (curl_easy_perform curl))
  167. (if (!= res CURLE_OK)
  168. (println (get-string (curl_easy_strerror res))))
  169. (curl_easy_cleanup curl))))
  170.  
  171. (define (curl-https url)
  172. (local (curl res)
  173. (setq curl (curl_easy_init))
  174. (when (!= curl 0)
  175. (curl_easy_setopt curl CURLOPT_URL url)
  176. ; (curl_easy_setopt curl CURLOPT_HEADER 1)
  177. (curl_easy_setopt curl CURLOPT_SSL_VERIFYPEER 0)
  178. ; (curl_easy_setopt curl CURLOPT_SSL_VERIFYHOST 0)
  179. (setf res (curl_easy_perform curl))
  180. (if (!= res CURLE_OK)
  181. (println (get-string (curl_easy_strerror res))))
  182. ;; always cleanup
  183. (curl_easy_cleanup curl)
  184. res)))
  185. ;(curl-https "https://mail.google.com/mail/")
  186.  
  187. (define (curl-writefunction url)
  188.  
  189. (define writeBuffer "")
  190. (define (recvf ptr size nmemb userp)
  191. ## fwrite(ptr, size, nmemb, (FILE *)userp);
  192. ;; (println (list ptr size nmemb userp))
  193. (let ((written (* size nmemb)))
  194. (extend writeBuffer (first (unpack (format "s%d" written) ptr)))
  195. (cpymem ptr (dup "x" written) written) ; XXX: dummy write
  196. ))
  197.  
  198. ;(curl_global_init CURL_GLOBAL_ALL)
  199. (let ((curl (curl_easy_init))
  200. (ret 0))
  201. (if (= curl 0)
  202. (throw-error (string "curl_easy_init=" curl)))
  203.  
  204. (curl_easy_setopt curl CURLOPT_URL url)
  205. (curl_easy_setopt curl CURLOPT_USERAGENT "Mozilla/5.0")
  206. (curl_easy_setopt curl CURLOPT_HEADER 0)
  207. (curl_easy_setopt curl CURLOPT_FOLLOWLOCATION 0)
  208. (curl_easy_setopt curl CURLOPT_NOPROGRESS 1)
  209. ;(curl_easy_setopt curl CURLOPT_POST 1)
  210. ;(curl_easy_setopt curl CURLOPT_POSTFIELDS "")
  211.  
  212. ;(curl_easy_setopt curl CURLOPT_WRITEDATA str)
  213. (curl_easy_setopt curl CURLOPT_WRITEFUNCTION (callback 0 'recvf))
  214. ;(curl_easy_setopt curl CURLOPT_ERRORBUFFER errBuffer)
  215.  
  216. (curl_easy_setopt curl CURLOPT_SSL_VERIFYPEER 0) ; option -k/--insecure
  217. ;(curl_easy_setopt curl CURLOPT_SSL_VERIFYHOST 0)
  218. ;(curl_easy_setopt curl CURLOPT_RETURNTRANSFER 1)
  219.  
  220. (setf ret (curl_easy_perform curl))
  221. (if (!= ret CURLE_OK)
  222. (println (get-string (curl_easy_strerror ret)))
  223. (write 1 writeBuffer))
  224.  
  225. (curl_easy_cleanup curl)
  226. ;(curl_global_cleanup)
  227. true))
  228.  
  229. (dolist (arg (main-args))
  230. (case arg
  231. ("-test"
  232. (curl-writefunction (or (main-args (+ $idx 1)) "localhost"))
  233. (exit))
  234. ))
  235.  
  236. (context MAIN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement