PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/netcdf/4.3.1/oc2/ochttp.c

https://bitbucket.org/vijaysm/libmesh
C | 334 lines | 261 code | 46 blank | 27 comment | 76 complexity | 27a84eb3534685881d1e4d51c1cf2e4b MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0
  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. off_t* 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. oclog(OCLOGNOTE,"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. oclog(OCLOGERR, "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. oclog(OCLOGWARN, "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. oclog(OCLOGNOTE,"buffersize: %lu bytes",(off_t)ocbyteslength(buf));
  112. #endif
  113. return OCTHROW(stat);
  114. fail:
  115. oclog(OCLOGERR, "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. oclog(OCLOGWARN,"WriteFileCallback: zero sized chunk");
  127. count = fwrite(ptr, size, nmemb, fetchdata->stream);
  128. if (count > 0) {
  129. fetchdata->size += (count * size);
  130. } else {
  131. oclog(OCLOGWARN,"WriteFileCallback: zero sized write");
  132. }
  133. #ifdef OCPROGRESS
  134. oclog(OCLOGNOTE,"callback: %lu bytes",(off_t)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. oclog(OCLOGWARN,"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. oclog(OCLOGNOTE,"callback: %lu bytes",(off_t)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. }
  211. if (curlp)
  212. *curlp = curl;
  213. return OCTHROW(stat);
  214. }
  215. void
  216. occurlclose(CURL* curl)
  217. {
  218. if (curl != NULL)
  219. curl_easy_cleanup(curl);
  220. }
  221. int
  222. ocfetchlastmodified(CURL* curl, char* url, long* filetime)
  223. {
  224. int stat = OC_NOERR;
  225. CURLcode cstat = CURLE_OK;
  226. /* Set the URL */
  227. cstat = curl_easy_setopt(curl, CURLOPT_URL, (void*)url);
  228. if (cstat != CURLE_OK)
  229. goto fail;
  230. /* Ask for head */
  231. cstat = curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30); /* 30sec timeout*/
  232. cstat = curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2);
  233. cstat = curl_easy_setopt(curl, CURLOPT_HEADER, 1);
  234. cstat = curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
  235. cstat = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
  236. cstat = curl_easy_setopt(curl, CURLOPT_FILETIME, (long)1);
  237. cstat = curl_easy_perform(curl);
  238. if(cstat != CURLE_OK) goto fail;
  239. if(filetime != NULL)
  240. cstat = curl_easy_getinfo(curl,CURLINFO_FILETIME,filetime);
  241. if(cstat != CURLE_OK) goto fail;
  242. return OCTHROW(stat);
  243. fail:
  244. oclog(OCLOGERR, "curl error: %s", curl_easy_strerror(cstat));
  245. return OCTHROW(OC_ECURL);
  246. }
  247. int
  248. ocping(const char* url)
  249. {
  250. int stat = OC_NOERR;
  251. CURLcode cstat = CURLE_OK;
  252. CURL* curl = NULL;
  253. OCbytes* buf = NULL;
  254. /* Create a CURL instance */
  255. stat = occurlopen(&curl);
  256. if(stat != OC_NOERR) return stat;
  257. /* use a very short timeout: 10 seconds */
  258. cstat = curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long)10);
  259. if (cstat != CURLE_OK)
  260. goto done;
  261. /* fail on HTTP 400 code errors */
  262. cstat = curl_easy_setopt(curl, CURLOPT_FAILONERROR, (long)1);
  263. if (cstat != CURLE_OK)
  264. goto done;
  265. /* Try to get the file */
  266. buf = ocbytesnew();
  267. stat = ocfetchurl(curl,url,buf,NULL);
  268. if(stat == OC_NOERR) {
  269. /* Don't trust curl to return an error when request gets 404 */
  270. long http_code = 0;
  271. cstat = curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE, &http_code);
  272. if (cstat != CURLE_OK)
  273. goto done;
  274. if(http_code >= 400) {
  275. cstat = CURLE_HTTP_RETURNED_ERROR;
  276. goto done;
  277. }
  278. } else
  279. goto done;
  280. done:
  281. ocbytesfree(buf);
  282. occurlclose(curl);
  283. if(cstat != CURLE_OK) {
  284. oclog(OCLOGERR, "curl error: %s", curl_easy_strerror(cstat));
  285. stat = OC_EDAPSVC;
  286. }
  287. return OCTHROW(stat);
  288. }