Example #1
0
url *url_init(char *raw_url) {
  parsed_url *parsed_url = parse_url(raw_url);

  url *result = copy_from_parsed_url(parsed_url);

  free_parsed_url(parsed_url);
  
  return result;
}
Example #2
0
int initscoreposter(char *url, char *user, int debugin) {
   struct curl_slist *list = NULL;
   url_parser_url_t *parsed_url;
   const char *https = "https:";
   char portstr[64];
   struct addrinfo hints;
   int i,rc;
   int bcast = 1;
   output.memory = malloc(1);
   output.size = 0;
   if (debugin) debug = fopen("curl.dbg","w");
   parsed_url=(url_parser_url_t*) malloc(sizeof(url_parser_url_t));
   if (parse_url(url,0,parsed_url) != 0) {
      if (debug != NULL) fprintf(debug,"URL parse failed\n");
      free(parsed_url);
      return -1;
   }
   for(i=0;i<parsed_url->protocol[i];i++) {
      parsed_url->protocol[i]=tolower(parsed_url->protocol[i]);
   }
   if (strcmp("udp",parsed_url->protocol) == 0) {
      if (debug != NULL) fprintf(debug,"Using UDP\n");
      memset(&hints,'\0',sizeof(hints));
      hints.ai_family = AF_UNSPEC;
      hints.ai_socktype = SOCK_DGRAM;
      hints.ai_protocol = 0;
      hints.ai_flags = AI_ADDRCONFIG;
      snprintf(portstr,strlen(portstr),"%d",parsed_url->port);
      rc = getaddrinfo(parsed_url->host,portstr,&hints,&result);
      free_parsed_url(parsed_url);
      if (rc != 0) {
         if (debug != NULL) fprintf(debug,"Could not resolve address\n");
         return -1;
      }
      for (rp=result;rp != NULL; rp=rp->ai_next) {
         fd=socket(rp->ai_family,rp->ai_socktype,rp->ai_protocol);
         if (fd == -1) continue;
         if (setsockopt(fd,SOL_SOCKET,SO_BROADCAST,&bcast,sizeof(bcast)) == 1) {
            if (debug != NULL) {
               fprintf(debug,"Could not set socket to broadcast\n");
            }
         }
         if (connect(fd,rp->ai_addr,rp->ai_addrlen) != -1) break;
         close(fd);
      }
      if (rp == NULL) {
         if (debug != NULL) {
            fprintf(debug,"Could not connect - %s\n",strerror(errno));
         }
         return -1;
      }

      if (debug != NULL) fflush(debug);
      return 0;
   } else {
      if (debug != 0) fprintf(debug,"Using curl\n");
      free_parsed_url(parsed_url);
      curl = curl_easy_init();
   }
   if (curl) {
      curl_easy_setopt(curl,CURLOPT_URL,url);
      curl_easy_setopt(curl,CURLOPT_STDERR,debug);
      curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_memory_callback);
      curl_easy_setopt(curl,CURLOPT_WRITEDATA, (void *) &output);
      if ((strncmp(url,https,strlen(https)) == 0) & (user != NULL)) {
         curl_easy_setopt(curl,CURLOPT_USERPWD,user);
      }
      curl_easy_setopt(curl,CURLOPT_POST,1L);
      curl_easy_setopt(curl,CURLOPT_USERAGENT,"TR linux");
      list = curl_slist_append(list,"Content-Type:text/xml");
      list = curl_slist_append(list,"Expect:");
      curl_easy_setopt(curl,CURLOPT_HTTPHEADER,list);
      curl_easy_setopt(curl,CURLOPT_VERBOSE,1L);
      curl_easy_setopt(curl,CURLOPT_READFUNCTION,read_callback);
      if (debug != NULL) fflush(debug);
      return 0;
   }
   return -1;
}