Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- #include <stdlib.h>
- #include <fcntl.h>
- #include <curl/curl.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include "gfserver.h"
- //Replace with an implementation of handle_with_curl and any other
- //functions you may need.
- /*
- * handle_with_curl.c - (modify) implement the handle_with_curl function here using the libcurl library.
- * On a 404 from the webserver, this function should return a header with a Getfile status of GF_FILE_NOT_FOUND.
- *
- */
- static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream){
- size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
- return written;
- }
- static size_t my_write_cb(gfcontext_t *ctx, char *path);
- struct worker_args{
- gfcontext_t *ctx;
- char *path;
- FILE *tmpfile;
- };
- ssize_t handle_with_curl(gfcontext_t *ctx, char *path, void* arg){
- // int fildes;
- // size_t file_len, bytes_transferred;
- // ssize_t read_len, write_len;
- // static const char *pagefilename = "page.out";
- // const char *tmpfile = path;
- FILE *pagefile;
- struct worker_args *wargs = (struct worker_args*) malloc(sizeof(struct worker_args));
- wargs->ctx = ctx;
- // arg is server variable from webproxy.c
- char buffer[4096];
- char *data_dir = arg;
- printf("Server: %s \n", data_dir);
- strcat(buffer, data_dir);
- strcat(buffer,path); //path passed to handle_with_curl() from workload.txt ?
- printf("Path: %s \n", path);
- printf("Buffer: %s \n", buffer);
- wargs->path = buffer;
- // strcat(buffer,path); //path passed to handle_with_curl() from workload.txt ?
- CURL *curl_handle;
- CURLcode cc;
- curl_global_init(CURL_GLOBAL_ALL);
- /* init the curl session */
- curl_handle = curl_easy_init();
- /* set URL to get here, I assume buffer to be the URL */
- curl_easy_setopt(curl_handle, CURLOPT_URL, buffer);
- // curl_easy_setopt(curl_handle, CURLOPT_URL, *data_dir);
- /* Switch on full protocol/debug output while testing */
- curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
- /* disable progress meter, set to 0L to enable and disable debug output */
- curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
- /* send all data to this function */
- curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
- /* open the file */
- // pagefile = fopen(pagefilename, "wb");
- pagefile = tmpfile();
- wargs->tmpfile = pagefile;
- if (pagefile) {
- /* write the page body to this file handle */
- /* Arguments ctx for my_write_cb() */
- puts("Curl**************************");
- curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);
- /* get it! */
- cc = curl_easy_perform(curl_handle);
- if(cc != CURLE_OK)
- fprintf(stderr, "curl_easy_perform() failed: %s\n",
- curl_easy_strerror(cc));
- /* close the header file */
- fclose(pagefile);
- }
- curl_easy_cleanup(curl_handle);
- return 0;
- }
- static size_t my_write_cb(gfcontext_t *ctx, char *path) {
- int fildes;
- size_t file_len, bytes_transferred;
- ssize_t read_len, write_len;
- char buffer[4096];
- strcpy(buffer, path);
- puts("Write cb 1");
- // Provided Code
- if (0 > (fildes = open(buffer, O_RDONLY))) {
- if (errno == ENOENT)
- /* If the file just wasn't found, then send FILE_NOT_FOUND code*/
- return gfs_sendheader(ctx, GF_FILE_NOT_FOUND, 0);
- else
- /* Otherwise, it must have been a server error. gfserver library will handle*/
- return EXIT_FAILURE;
- }
- puts("Write cb 2");
- /* Calculating the file size */
- file_len = lseek(fildes, 0, SEEK_END);
- lseek(fildes, 0, SEEK_SET);
- gfs_sendheader(ctx, GF_OK, file_len);
- /* Sending the file contents chunk by chunk. */
- bytes_transferred = 0;
- while (bytes_transferred < file_len) {
- read_len = read(fildes, buffer, 4096);
- if (read_len <= 0) {
- fprintf(stderr, "handle_with_file read error, %zd, %zu, %zu",
- read_len, bytes_transferred, file_len);
- return EXIT_FAILURE;
- }
- write_len = gfs_send(ctx, buffer, read_len);
- if (write_len != read_len) {
- fprintf(stderr, "handle_with_file write error");
- return EXIT_FAILURE;
- }
- bytes_transferred += write_len;
- }
- return bytes_transferred;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement