ochttp.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc.
  2. See the COPYRIGHT file for more information. */
  3. #include "config.h"
  4. #include <sys/stat.h>
  5. #ifdef HAVE_UNISTD_H
  6. #include <unistd.h>
  7. #endif
  8. #include <fcntl.h>
  9. #include "ocinternal.h"
  10. #include "ocdebug.h"
  11. #include "ochttp.h"
  12. #include "ocrc.h"
  13. static size_t WriteFileCallback(void*, size_t, size_t, void*);
  14. static size_t WriteMemoryCallback(void*, size_t, size_t, void*);
  15. struct Fetchdata {
  16. FILE* stream;
  17. size_t size;
  18. };
  19. long
  20. ocfetchhttpcode(CURL* curl)
  21. {
  22. long httpcode;
  23. CURLcode cstat = CURLE_OK;
  24. /* Extract the http code */
  25. cstat = curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE,&httpcode);
  26. if(cstat != CURLE_OK) httpcode = 0;
  27. return httpcode;
  28. }
  29. int
  30. ocfetchurl_file(CURL* curl, const char* url, FILE* stream,
  31. unsigned long* sizep, long* filetime)
  32. {
  33. int stat = OC_NOERR;
  34. CURLcode cstat = CURLE_OK;
  35. struct Fetchdata fetchdata;
  36. /* Set the URL */
  37. cstat = curl_easy_setopt(curl, CURLOPT_URL, (void*)url);
  38. if (cstat != CURLE_OK)
  39. goto fail;
  40. /* send all data to this function */
  41. cstat = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteFileCallback);
  42. if (cstat != CURLE_OK)
  43. goto fail;
  44. /* we pass our file to the callback function */
  45. cstat = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&fetchdata);
  46. if (cstat != CURLE_OK)
  47. goto fail;
  48. /* One last thing; always try to get the last modified time */
  49. cstat = curl_easy_setopt(curl, CURLOPT_FILETIME, (long)1);
  50. if (cstat != CURLE_OK)
  51. goto fail;
  52. fetchdata.stream = stream;
  53. fetchdata.size = 0;
  54. cstat = curl_easy_perform(curl);
  55. if (cstat != CURLE_OK)
  56. goto fail;
  57. if (stat == OC_NOERR) {
  58. /* return the file size*/
  59. #ifdef OCDEBUG
  60. oc_log(LOGNOTE,"filesize: %lu bytes",fetchdata.size);
  61. #endif
  62. if (sizep != NULL)
  63. *sizep = fetchdata.size;
  64. /* Get the last modified time */
  65. if(filetime != NULL)
  66. cstat = curl_easy_getinfo(curl,CURLINFO_FILETIME,filetime);
  67. if(cstat != CURLE_OK) goto fail;
  68. }
  69. return OCTHROW(stat);
  70. fail:
  71. oc_log(LOGERR, "curl error: %s", curl_easy_strerror(cstat));
  72. return OCTHROW(OC_ECURL);
  73. }
  74. int
  75. ocfetchurl(CURL* curl, const char* url, OCbytes* buf, long* filetime)
  76. {
  77. int stat = OC_NOERR;
  78. CURLcode cstat = CURLE_OK;
  79. size_t len;
  80. /* Set the URL */
  81. cstat = curl_easy_setopt(curl, CURLOPT_URL, (void*)url);
  82. if (cstat != CURLE_OK)
  83. goto fail;
  84. /* send all data to this function */
  85. cstat = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  86. if (cstat != CURLE_OK)
  87. goto fail;
  88. /* we pass our file to the callback function */
  89. cstat = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)buf);
  90. if (cstat != CURLE_OK)
  91. goto fail;
  92. /* One last thing; always try to get the last modified time */
  93. cstat = curl_easy_setopt(curl, CURLOPT_FILETIME, (long)1);
  94. cstat = curl_easy_perform(curl);
  95. if(cstat == CURLE_PARTIAL_FILE) {
  96. /* Log it but otherwise ignore */
  97. oc_log(LOGWARN, "curl error: %s; ignored",
  98. curl_easy_strerror(cstat));
  99. cstat = CURLE_OK;
  100. }
  101. if(cstat != CURLE_OK) goto fail;
  102. /* Get the last modified time */
  103. if(filetime != NULL)
  104. cstat = curl_easy_getinfo(curl,CURLINFO_FILETIME,filetime);
  105. if(cstat != CURLE_OK) goto fail;
  106. /* Null terminate the buffer*/
  107. len = ocbyteslength(buf);
  108. ocbytesappend(buf, '\0');
  109. ocbytessetlength(buf, len); /* dont count null in buffer size*/
  110. #ifdef OCDEBUG
  111. oc_log(LOGNOTE,"buffersize: %lu bytes",(unsigned long)ocbyteslength(buf));
  112. #endif
  113. return OCTHROW(stat);
  114. fail:
  115. oc_log(LOGERR, "curl error: %s", curl_easy_strerror(cstat));
  116. return OCTHROW(OC_ECURL);
  117. }
  118. static size_t
  119. WriteFileCallback(void* ptr, size_t size, size_t nmemb, void* data)
  120. {
  121. size_t realsize = size * nmemb;
  122. size_t count;
  123. struct Fetchdata* fetchdata;
  124. fetchdata = (struct Fetchdata*) data;
  125. if(realsize == 0)
  126. oc_log(LOGWARN,"WriteFileCallback: zero sized chunk");
  127. count = fwrite(ptr, size, nmemb, fetchdata->stream);
  128. if (count > 0) {
  129. fetchdata->size += (count * size);
  130. } else {
  131. oc_log(LOGWARN,"WriteFileCallback: zero sized write");
  132. }
  133. #ifdef OCPROGRESS
  134. oc_log(LOGNOTE,"callback: %lu bytes",(unsigned long)realsize);
  135. #endif
  136. return count;
  137. }
  138. static size_t
  139. WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
  140. {
  141. size_t realsize = size * nmemb;
  142. OCbytes* buf = (OCbytes*) data;
  143. if(realsize == 0)
  144. oc_log(LOGWARN,"WriteMemoryCallback: zero sized chunk");
  145. /* Optimize for reading potentially large dods datasets */
  146. if(!ocbytesavail(buf,realsize)) {
  147. /* double the size of the packet */
  148. ocbytessetalloc(buf,2*ocbytesalloc(buf));
  149. }
  150. ocbytesappendn(buf, ptr, realsize);
  151. #ifdef OCPROGRESS
  152. oc_log(LOGNOTE,"callback: %lu bytes",(unsigned long)realsize);
  153. #endif
  154. return realsize;
  155. }
  156. #if 0
  157. static void
  158. assembleurl(DAPURL* durl, OCbytes* buf, int what)
  159. {
  160. encodeurltext(durl->url,buf);
  161. if(what & WITHPROJ) {
  162. ocbytescat(buf,"?");
  163. encodeurltext(durl->projection,buf);
  164. }
  165. if(what & WITHSEL) encodeurltext(durl->selection,buf);
  166. }
  167. static char mustencode="";
  168. static char hexchars[16] = {
  169. '0', '1', '2', '3',
  170. '4', '5', '6', '7',
  171. '8', '9', 'a', 'b',
  172. 'c', 'd', 'e', 'f',
  173. };
  174. static void
  175. encodeurltext(char* text, OCbytes* buf)
  176. {
  177. /* Encode the URL to handle illegal characters */
  178. len = strlen(url);
  179. encoded = ocmalloc(len*4+1); /* should never be larger than this*/
  180. if(encoded==NULL) return;
  181. p = url; q = encoded;
  182. while((c=*p++)) {
  183. if(strchr(mustencode,c) != NULL) {
  184. char tmp[8];
  185. int hex1, hex2;
  186. hex1 = (c & 0x0F);
  187. hex2 = (c & 0xF0) >> 4;
  188. tmp[0] = '0'; tmp[1] = 'x';
  189. tmp[2] = hexchars[hex2]; tmp[3] = hexchars[hex1];
  190. tmp[4] = '\0';
  191. ocbytescat(buf,tmp);
  192. } else *q++ = (char)c;
  193. }
  194. }
  195. #endif
  196. int
  197. occurlopen(CURL** curlp)
  198. {
  199. int stat = OC_NOERR;
  200. CURLcode cstat = CURLE_OK;
  201. CURL* curl;
  202. /* initialize curl*/
  203. curl = curl_easy_init();
  204. if (curl == NULL)
  205. stat = OC_ECURL;
  206. else {
  207. cstat = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
  208. if (cstat != CURLE_OK)
  209. stat = OC_ECURL;
  210. /* some servers don't like requests that are made without a user-agent */
  211. cstat = curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
  212. if (cstat != CURLE_OK)
  213. stat = OC_ECURL;
  214. }
  215. if (curlp)
  216. *curlp = curl;
  217. return OCTHROW(stat);
  218. }
  219. void
  220. occurlclose(CURL* curl)
  221. {
  222. if (curl != NULL)
  223. curl_easy_cleanup(curl);
  224. }
  225. int
  226. ocfetchlastmodified(CURL* curl, char* url, long* filetime)
  227. {
  228. int stat = OC_NOERR;
  229. CURLcode cstat = CURLE_OK;
  230. /* Set the URL */
  231. cstat = curl_easy_setopt(curl, CURLOPT_URL, (void*)url);
  232. if (cstat != CURLE_OK)
  233. goto fail;
  234. /* Ask for head */
  235. cstat = curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30); /* 30sec timeout*/
  236. cstat = curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2);
  237. cstat = curl_easy_setopt(curl, CURLOPT_HEADER, 1);
  238. cstat = curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
  239. cstat = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
  240. cstat = curl_easy_setopt(curl, CURLOPT_FILETIME, (long)1);
  241. cstat = curl_easy_perform(curl);
  242. if(cstat != CURLE_OK) goto fail;
  243. if(filetime != NULL)
  244. cstat = curl_easy_getinfo(curl,CURLINFO_FILETIME,filetime);
  245. if(cstat != CURLE_OK) goto fail;
  246. return OCTHROW(stat);
  247. fail:
  248. oc_log(LOGERR, "curl error: %s", curl_easy_strerror(cstat));
  249. return OCTHROW(OC_ECURL);
  250. }
  251. int
  252. ocping(const char* url)
  253. {
  254. int stat = OC_NOERR;
  255. CURLcode cstat = CURLE_OK;
  256. CURL* curl = NULL;
  257. OCbytes* buf = NULL;
  258. /* Create a CURL instance */
  259. stat = occurlopen(&curl);
  260. if(stat != OC_NOERR) return stat;
  261. /* use a very short timeout: 10 seconds */
  262. cstat = curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long)10);
  263. if (cstat != CURLE_OK)
  264. goto done;
  265. /* fail on HTTP 400 code errors */
  266. cstat = curl_easy_setopt(curl, CURLOPT_FAILONERROR, (long)1);
  267. if (cstat != CURLE_OK)
  268. goto done;
  269. /* Try to get the file */
  270. buf = ocbytesnew();
  271. stat = ocfetchurl(curl,url,buf,NULL);
  272. if(stat == OC_NOERR) {
  273. /* Don't trust curl to return an error when request gets 404 */
  274. long http_code = 0;
  275. cstat = curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE, &http_code);
  276. if (cstat != CURLE_OK)
  277. goto done;
  278. if(http_code >= 400) {
  279. cstat = CURLE_HTTP_RETURNED_ERROR;
  280. goto done;
  281. }
  282. } else
  283. goto done;
  284. done:
  285. ocbytesfree(buf);
  286. occurlclose(curl);
  287. if(cstat != CURLE_OK) {
  288. oc_log(LOGERR, "curl error: %s", curl_easy_strerror(cstat));
  289. stat = OC_EDAPSVC;
  290. }
  291. return OCTHROW(stat);
  292. }