Advertisement
Guest User

Code discussion

a guest
Jun 30th, 2015
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. Can you see what I am doing conceptually, I feel lost . i have two write functions only because I trying to debug something. I eventually should only have one I think.
  2.  
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <curl/curl.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9.  
  10. #include "gfserver.h"
  11.  
  12. //Replace with an implementation of handle_with_curl and any other
  13. //functions you may need.
  14.  
  15. /*
  16. * handle_with_curl.c - (modify) implement the handle_with_curl function here using the libcurl library.
  17. * On a 404 from the webserver, this function should return a header with a Getfile status of GF_FILE_NOT_FOUND.
  18. *
  19. */
  20.  
  21. static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream){
  22. size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
  23. return written;
  24. }
  25.  
  26. static size_t my_write_cb(gfcontext_t *ctx, char *path);
  27.  
  28. struct worker_args{
  29. gfcontext_t *ctx;
  30. char *path;
  31. FILE *tmpfile;
  32.  
  33.  
  34. };
  35.  
  36. ssize_t handle_with_curl(gfcontext_t *ctx, char *path, void* arg){
  37. // int fildes;
  38. // size_t file_len, bytes_transferred;
  39. // ssize_t read_len, write_len;
  40. // static const char *pagefilename = "page.out";
  41. // const char *tmpfile = path;
  42. FILE *pagefile;
  43.  
  44.  
  45.  
  46. struct worker_args *wargs = (struct worker_args*) malloc(sizeof(struct worker_args));
  47. wargs->ctx = ctx;
  48.  
  49.  
  50.  
  51.  
  52.  
  53. // arg is server variable from webproxy.c
  54.  
  55. char buffer[4096];
  56. char *data_dir = arg;
  57. printf("Server: %s \n", data_dir);
  58. strcat(buffer, data_dir);
  59. strcat(buffer,path); //path passed to handle_with_curl() from workload.txt ?
  60. printf("Path: %s \n", path);
  61. printf("Buffer: %s \n", buffer);
  62.  
  63. wargs->path = buffer;
  64.  
  65.  
  66.  
  67. // strcat(buffer,path); //path passed to handle_with_curl() from workload.txt ?
  68.  
  69.  
  70.  
  71. CURL *curl_handle;
  72. CURLcode cc;
  73.  
  74. curl_global_init(CURL_GLOBAL_ALL);
  75.  
  76. /* init the curl session */
  77. curl_handle = curl_easy_init();
  78.  
  79. /* set URL to get here, I assume buffer to be the URL */
  80. curl_easy_setopt(curl_handle, CURLOPT_URL, buffer);
  81. // curl_easy_setopt(curl_handle, CURLOPT_URL, *data_dir);
  82.  
  83. /* Switch on full protocol/debug output while testing */
  84. curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
  85.  
  86. /* disable progress meter, set to 0L to enable and disable debug output */
  87. curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
  88. /* send all data to this function */
  89. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
  90.  
  91. /* open the file */
  92. // pagefile = fopen(pagefilename, "wb");
  93. pagefile = tmpfile();
  94.  
  95. wargs->tmpfile = pagefile;
  96.  
  97. if (pagefile) {
  98.  
  99. /* write the page body to this file handle */
  100. /* Arguments ctx for my_write_cb() */
  101. puts("Curl**************************");
  102.  
  103. curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);
  104.  
  105. /* get it! */
  106. cc = curl_easy_perform(curl_handle);
  107. if(cc != CURLE_OK)
  108. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  109. curl_easy_strerror(cc));
  110.  
  111. /* close the header file */
  112. fclose(pagefile);
  113. }
  114. curl_easy_cleanup(curl_handle);
  115. return 0;
  116.  
  117. }
  118.  
  119. static size_t my_write_cb(gfcontext_t *ctx, char *path) {
  120.  
  121. int fildes;
  122. size_t file_len, bytes_transferred;
  123. ssize_t read_len, write_len;
  124. char buffer[4096];
  125.  
  126. strcpy(buffer, path);
  127.  
  128. puts("Write cb 1");
  129.  
  130.  
  131. // Provided Code
  132. if (0 > (fildes = open(buffer, O_RDONLY))) {
  133. if (errno == ENOENT)
  134. /* If the file just wasn't found, then send FILE_NOT_FOUND code*/
  135. return gfs_sendheader(ctx, GF_FILE_NOT_FOUND, 0);
  136. else
  137. /* Otherwise, it must have been a server error. gfserver library will handle*/
  138. return EXIT_FAILURE;
  139. }
  140.  
  141.  
  142. puts("Write cb 2");
  143.  
  144. /* Calculating the file size */
  145. file_len = lseek(fildes, 0, SEEK_END);
  146. lseek(fildes, 0, SEEK_SET);
  147.  
  148. gfs_sendheader(ctx, GF_OK, file_len);
  149.  
  150. /* Sending the file contents chunk by chunk. */
  151. bytes_transferred = 0;
  152. while (bytes_transferred < file_len) {
  153. read_len = read(fildes, buffer, 4096);
  154. if (read_len <= 0) {
  155. fprintf(stderr, "handle_with_file read error, %zd, %zu, %zu",
  156. read_len, bytes_transferred, file_len);
  157. return EXIT_FAILURE;
  158. }
  159. write_len = gfs_send(ctx, buffer, read_len);
  160. if (write_len != read_len) {
  161. fprintf(stderr, "handle_with_file write error");
  162. return EXIT_FAILURE;
  163. }
  164. bytes_transferred += write_len;
  165. }
  166.  
  167. return bytes_transferred;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement