C-код для клиента http-webdav с использованием CURLlibrary - PullRequest
0 голосов
/ 20 ноября 2018

Я ищу код c для клиента http-webdav с использованием библиотеки CURL с включенной аутентификацией.

Я пробовал использовать некоторые примеры кодов, предоставляемых CURL, но, похоже, ничего не проходит методы аутентификациии работа.

Я включил дайджест-проверку подлинности с помощью пользователя и пароля.Запрос клиента отправляется правильно, но ответ сервера - 301. Я перехватил пакеты, используя wireshark.Есть что-то, чего не хватает?

Ниже приведен код, который я пробовал.

/* read callback function, fread() look alike */
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
  ssize_t retcode;
  curl_off_t nread;

  int *fdp = (int *)stream;
  int fd = *fdp;

  retcode = read(fd, ptr, size * nmemb);

  nread = (curl_off_t)retcode;

  fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
          " bytes from file\n", nread);

  return retcode;
}


/*
 * This example shows a HTTP PUT operation. PUTs a file given as a command
 * line argument to the URL also given on the command line.
 *
 * This example also uses its own read callback.
 *
 * Here's an article on how to setup a PUT handler for Apache:
 * http://www.apacheweek.com/features/put
 */ 


int curlApache ()
{
  CURL *curl;
  CURLcode res;
  FILE * hd_src;
  struct stat file_info;

  char *file;
  char *url;

  char error;

  file = "/bd0/filecreate.txt";
  url = "http://10.1.20.127/sites/";
  //url = "http://10.1.21.14/webdav/test.txt";

  struct curl_slist *slist = NULL; 
  slist = curl_slist_append(slist, "Accept: text/xml"); 
  slist = curl_slist_append(slist, "Depth: infinity"); 
  slist = curl_slist_append(slist, "Connection: Keep-Alive"); 
  slist = curl_slist_append(slist, "Content-Type: text/xml"); 
  slist = curl_slist_append(slist, "Expect:"); 

  /* get the file size of the local file */ 
  stat(file, &file_info);

  hd_src = fopen(file, "a+");
  if (hd_src == NULL)             
  printf("Disc full or no permission\n");



  const char *str = "This is the file content";
  const char read[24];
      if (hd_src != NULL) 
      if (fputs (str, hd_src) != EOF); 
      if( fgets (read, 24, hd_src)!=NULL ) {
            /* writing content to stdout */
            puts(read);
         }

  /* In windows, this will init the winsock stuff */ 
  curl_global_init(CURL_GLOBAL_ALL);

  /* get a curl handle */ 
  curl = curl_easy_init();
  if(curl) {

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 3L);

    /* we want to use our own read function */ 
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);

    /* enable uploading */ 
    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

    /* HTTP PUT please */ 
    curl_easy_setopt(curl, CURLOPT_PUT, 1L);

    /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
            also costs one extra round-trip and possibly sending of all the PUT
            data twice!!! */
    curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_DIGEST); 


   curl_easy_setopt(curl, CURLOPT_USERPWD, "admin:nimo0630");

    fseek(hd_src, 0L, SEEK_END);
    int file_size;
    file_size = ftell(hd_src);

    Curl_setopt(curl, CURLOPT_INFILE, hd_src);
    Curl_setopt(curl, CURLOPT_INFILESIZE, file_size);

    /* specify target URL, and note that this URL should include a file
       name, not only a directory */  

    curl_easy_setopt(curl, CURLOPT_URL, url);

    /* now specify which file to upload */ 
    curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);

    /* provide the size of the upload, we specicially typecast the value
       to curl_off_t since we must be sure to use the correct data size */ 
    curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);

    /* Now run off and do what you've been told! */ 
    res = curl_easy_perform(curl);
    /* Check for errors */ 
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));


        if(!res) {
          /* extract the available authentication types */
        long auth;
        res = curl_easy_getinfo(curl, CURLINFO_HTTPAUTH_AVAIL, &auth);
        if(!res) 
        {
            if(!auth)
            printf("No auth available, perhaps no 401?\n");
            else
            {     
                printf("%s%s%s%s\n", \
                    auth & CURLAUTH_BASIC ? "Basic ":"", \
                    auth & CURLAUTH_DIGEST ? "Digest ":"", \
                    auth & CURLAUTH_NEGOTIATE ? "Negotiate ":"", \
                    auth % CURLAUTH_NTLM ? "NTLM ":"");
            }

        }
        }
    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  fclose(hd_src); /* close the local file */ 

  curl_global_cleanup();
  return 0;
}
...