Example #1
0
CURLcode backend_log(char * middleware, char * uuid, struct timeval tv, float value) {
	CURL *curl;
	CURLcode res;

	char url[255], useragent[255], post[255];

	sprintf(url, "%s/data/%s.json", middleware, uuid); /* build url */
	sprintf(useragent, "vzlogger/%s (%s)", VZ_VERSION, curl_version());
	sprintf(post, "?timestamp=%lu%lu&value=%f", tv.tv_sec, tv.tv_usec, value);
 
	curl_global_init(CURL_GLOBAL_ALL);
 
	curl_formadd(&formpost,
		&lastptr,
		CURLFORM_COPYNAME, "value",
		CURLFORM_PTRCONTENTS , value_str,
		CURLFORM_END);
		
	curl_formadd(&formpost,
		&lastptr,
		CURLFORM_COPYNAME, "timestamp",
		CURLFORM_PTRCONTENTS , &timestamp,
		CURLFORM_END);
 
	curl = curl_easy_init();
	
	if (curl) {
		/* what URL that receives this POST */ 
		curl_easy_setopt(curl, CURLOPT_URL, url);
		curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
		curl_easy_setopt(curl, CURLOPT_USERAGENT, useragent);
		curl_easy_setopt(curl, CURLOPT_VERBOSE, (int) opt.verbose);

    		res = curl_easy_perform(curl);
    
		curl_easy_cleanup(curl); /* always cleanup */ 
		curl_formfree(formpost); /* then cleanup the formpost chain */
		
		return res;
	}
	
	return -1;
}
Example #2
0
/* CURL Add in a string parameter */
int ov_curl_set_string(void *ov_msg_handle, const char *name, char *val)
{
	ov_msg_t *ov_msg = (ov_msg_t*) ov_msg_handle;
	assert(ov_msg->magic_no == OV_MSG_MAGIC_NO);

	printf("SET MULTIPART ENTRY: NAME = '%s' VAL = '%s'\n", name, val);

	curl_formadd(&ov_msg->formpost, &ov_msg->lastptr, CURLFORM_COPYNAME, name, CURLFORM_COPYCONTENTS, val, CURLFORM_END);
	return 0;
}
Example #3
0
static int formadd_files(VALUE data_key, VALUE data_value, VALUE self) {
  struct curl_state *state = get_curl_state(self);
  VALUE name = rb_obj_as_string(data_key);
  VALUE value = rb_obj_as_string(data_value);

  curl_formadd(&state->post, &state->last, CURLFORM_PTRNAME, RSTRING_PTR(name),
                CURLFORM_FILE, RSTRING_PTR(value), CURLFORM_END);

  return 0;
}
Example #4
0
  bool HTTP::POSTFile(std::string url, std::string filePath, std::string& output) throw (InputException) {
    char errorBuffer[CURL_ERROR_SIZE];
    std::string http_code;
    std::string redirect = url;
    char *ct = NULL;

    // wait for timeout
    wait();

    // error detection
    CURLcode result;

    //open connection
    CURL * curlPost = curl_easy_init();

    struct curl_httppost *post = NULL;
    struct curl_httppost *last = NULL;

    if (curlPost) {
      curl_formadd(&post, &last, CURLFORM_COPYNAME, "file", CURLFORM_FILE, filePath.c_str(), CURLFORM_END);
      //Specify the API Endpoint
      curl_easy_setopt(curlPost, CURLOPT_URL, url.c_str());
      //Specify the HTTP Method
      result = curl_easy_setopt(curlPost, CURLOPT_HTTPPOST, post);

      //      curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
      //      curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
      //      curl_easy_setopt(curl, CURLOPT_HEADER, 1);
      //      /**
      //       * Post Redirection - if we cant set this, the next redir is GET method
      //      0 -> do not set any behavior
      //      1 -> follow redirect with the same type of request only for 301 redirects.
      //      2 -> follow redirect with the same type of request only for 302 redirects.
      //      3 -> follow redirect with the same type of request both for 301 and 302 redirects.*/
      //            curl_easy_setopt(curl, CURLOPT_POSTREDIR, 3);
      //
      curl_easy_setopt(curlPost, CURLOPT_WRITEFUNCTION, &writeText);
      curl_easy_setopt(curlPost, CURLOPT_WRITEDATA, &output);
      //      curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);

      // send request
      result = curl_easy_perform(curlPost);

      // clean      
      curl_easy_cleanup(curlPost);

      // exception on request error
      if (result != CURLE_OK) {
        InputException e("HTTP POST File: Curl Error: " + std::string(errorBuffer));
        throw e;
      }
    }
    curl_formfree(post);
    return true;
  }
int curlization(char *uri) {

 int ret=0;
 buf->size=0;
 curl_easy_setopt(curl, CURLOPT_USERAGENT, UserAgent);
 curl_easy_setopt(curl, CURLOPT_URL, uri);

#ifdef HEADFUNC
 bufhead->size=0;
 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, head_callback);
#else
 curl_easy_setopt(curl, CURLOPT_HEADER, 1);
#endif  
 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);


//можно так
#ifdef FORMPOST
 curl_formadd(&post, &last, CURLFORM_COPYNAME, "hash",CURLFORM_PTRCONTENTS, "aaaaaaaaaaaa", CURLFORM_END);
 curl_formadd(&post, &last, CURLFORM_COPYNAME, "hash2",CURLFORM_PTRCONTENTS, "bbbbbbbbbbbbb", CURLFORM_END);
 curl_formadd(&post, &last, CURLFORM_COPYNAME, "hash3",CURLFORM_PTRCONTENTS, "cccccccccccccccc", CURLFORM_END);
 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
#else
 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Post);
 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, sizeof(Post)-1);
#endif

 if (curl_easy_perform(curl) != CURLE_OK) {
      fprintf(stderr, "%s: %s\n\n", uri, (error[0]) ? error : "Unknown Error");
      ret=-1;
  }


  curl_easy_cleanup(curl);

#ifdef FORMPOST
  curl_formfree(post);
#endif

 return ret;
}
Example #6
0
static optional<string> curlUpload(const char* path, const char* url) {

  struct curl_httppost *formpost=NULL;
  struct curl_httppost *lastptr=NULL;

  curl_global_init(CURL_GLOBAL_ALL);

  curl_formadd(&formpost,
      &lastptr,
      CURLFORM_COPYNAME, "fileToUpload",
      CURLFORM_FILE, path,
      CURLFORM_END);

  curl_formadd(&formpost,
      &lastptr,
      CURLFORM_COPYNAME, "submit",
      CURLFORM_COPYCONTENTS, "send",
      CURLFORM_END);

  if(CURL* curl = curl_easy_init()) {
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, dataFun);
    string ret;
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ret);
    /* what URL that receives this POST */ 
    curl_easy_setopt(curl, CURLOPT_URL, escapeUrl(url).c_str());
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
    // Internal CURL progressmeter must be disabled if we provide our own callback
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
    // Install the callback function
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progressFunction);
    CURLcode res = curl_easy_perform(curl);
    if(res != CURLE_OK)
      ret = string("Upload failed: ") + curl_easy_strerror(res);
    curl_easy_cleanup(curl);
    curl_formfree(formpost);
    if (!ret.empty())
      return ret;
    else
      return none;
  } else
    return string("Failed to initialize libcurl");
}
Example #7
0
	void HttpRequest::AddFile(const std::string& name, const std::string& localfilename,
		const std::string& contenttype, const std::string& filename)
	{
		curl_formadd(&m_postargs, &m_postargslast,
			CURLFORM_COPYNAME, name.c_str(),
			CURLFORM_FILE, localfilename.c_str(),
			CURLFORM_CONTENTTYPE, contenttype.c_str(),
			CURLFORM_END);

		m_upload = true;
	}
	void sendChatAction(int chat_id, string action)
	{
		string URL = BOTAPI + "sendChatAction";
		// ============ convert string to char array =========== // 
		char c[200];
		strncpy(c, URL.c_str(), sizeof(c));
		c[sizeof(c) - 1] = 0;
		
		FILE *fp;
		CURL *curl = init(c, "sendChatAction.json", fp);
		// ============ add fields =========== // 
		curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "chat_id", CURLFORM_COPYCONTENTS, to_string(chat_id).c_str(), CURLFORM_END);
		curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "action", CURLFORM_COPYCONTENTS, action.c_str(), CURLFORM_END);
		curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
		curl_easy_perform(curl);
	    curl_easy_cleanup(curl);
		formpost = NULL;
		lastptr = NULL;
		fclose(fp);
	}
Example #9
0
static int formadd_values(VALUE data_key, VALUE data_value, VALUE self) {
  struct curl_state *state;
  Data_Get_Struct(self, struct curl_state, state);

  VALUE name = rb_obj_as_string(data_key);
  VALUE value = rb_obj_as_string(data_value);

  curl_formadd(&state->post, &state->last, CURLFORM_PTRNAME, RSTRING_PTR(name),
                CURLFORM_PTRCONTENTS, RSTRING_PTR(value), CURLFORM_END);  
  return 0;
}
Example #10
0
void engine::attach(std::string server,
        const std::string& key,
        const std::string& file,
        const std::string& filename,
        int chunk_id,
        int num_chunks,
        report_level s,
        validate_cert v)
{
    init_curl(key, s, v);
    server += "/attachments";
    curl_easy_setopt(m_curl, CURLOPT_URL, server.c_str());
    struct curl_httppost* formpost = NULL;
    struct curl_httppost* lastptr = NULL;
    curl_formadd(&formpost,
            &lastptr,
            CURLFORM_COPYNAME, "Filedata",
            CURLFORM_FILE, file.c_str(),
            CURLFORM_END);
    curl_formadd(&formpost,
            &lastptr,
            CURLFORM_COPYNAME, "name",
            CURLFORM_COPYCONTENTS, filename.c_str(),
            CURLFORM_END);
    curl_formadd(&formpost,
            &lastptr,
            CURLFORM_COPYNAME, "chunk",
            CURLFORM_COPYCONTENTS, base::to_string(chunk_id).c_str(),
            CURLFORM_END);
    curl_formadd(&formpost,
            &lastptr,
            CURLFORM_COPYNAME, "chunks",
            CURLFORM_COPYCONTENTS, base::to_string(num_chunks).c_str(),
            CURLFORM_END);
    curl_form_guard fg(formpost);
    curl_easy_setopt(m_curl, CURLOPT_HTTPPOST, formpost);
    if (s >= NORMAL) {
        io::mout << "Uploading chunk '" << file << "'." << io::endl;
    }
    process_attach_chunk_responce(perform(), s);
}
	void setWebhook(string url, string certificate)
	{
		string URL = BOTAPI + "setWebhook";
		// ============ convert string to char array =========== // 
		char c[200];
		strncpy(c, URL.c_str(), sizeof(c));
		c[sizeof(c) - 1] = 0;
		
		FILE *fp;
		CURL *curl = init(c, "setWebhook.json", fp);
		// ============ add fields =========== // 
		curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "url", CURLFORM_COPYCONTENTS, url.c_str(), CURLFORM_END);
		if (certificate != "")
			curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "certificate", CURLFORM_FILE, certificate.c_str(), CURLFORM_END);
		curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
		curl_easy_perform(curl);
	    curl_easy_cleanup(curl);
		formpost = NULL;
		lastptr = NULL;
		fclose(fp);
	}
	void getUpdates(int offset, int limit, int timeout)
	{
		string URL = BOTAPI + "getUpdates";
		// ============ convert string to char array =========== // 
		char c[200];
		strncpy(c, URL.c_str(), sizeof(c));
		c[sizeof(c) - 1] = 0;
		
		FILE *fp;
		CURL *curl = init(c, "getUpdates.json", fp);
		// ============ add fields =========== // 
		curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "offset", CURLFORM_COPYCONTENTS, to_string(offset).c_str(), CURLFORM_END);
		curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "limit", CURLFORM_COPYCONTENTS, to_string(limit).c_str(), CURLFORM_END);
		curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "timeout", CURLFORM_COPYCONTENTS, to_string(timeout).c_str(), CURLFORM_END);
		curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
		curl_easy_perform(curl);
	    curl_easy_cleanup(curl);
		formpost = NULL;
		lastptr = NULL;
		fclose(fp);
	}
Example #13
0
void FZHttpPost::AddKeyValuePair(const std::string& key, const std::string& value)
{
   // Add a key-value pair to the POST

   curl_formadd(&formpost,
                &lastptr,
                CURLFORM_COPYNAME, key.data(),
                CURLFORM_COPYCONTENTS, value.data(),
                CURLFORM_END);
   // increment number of key-value pairs in post
   ++fPostPairs;
}
	void forwardMessage(int chat_id, int from_chat_id, int message_id)
	{
		string URL = BOTAPI + "forwardMessage";
		// ============ convert string to char array =========== // 
		char c[200];
		strncpy(c, URL.c_str(), sizeof(c));
		c[sizeof(c) - 1] = 0;
		
		FILE *fp;
		CURL *curl = init(c, "forwardMessage.json", fp);
		// ============ add fields =========== // 
		curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "chat_id", CURLFORM_COPYCONTENTS, to_string(chat_id).c_str(), CURLFORM_END);
		curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "from_chat_id", CURLFORM_COPYCONTENTS, to_string(from_chat_id).c_str(), CURLFORM_END);
		curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "message_id", CURLFORM_COPYCONTENTS, to_string(message_id).c_str(), CURLFORM_END);
		curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
		curl_easy_perform(curl);
	    curl_easy_cleanup(curl);
		formpost = NULL;
		lastptr = NULL;
		fclose(fp);
	}
void HttpFormRequest::addBinary(const std::string &key, const std::vector<char> &data) {
    std::cout << "ttpFormRequest::addBinary(): " << data.data() << "\t" << data.size() << std::endl;

    curl_formadd(
        &_formPost,
        &_lastPost,
        CURLFORM_COPYNAME, key.c_str(),
        CURLFORM_BUFFER, key.c_str(),
        CURLFORM_BUFFERPTR, data.data(),
        CURLFORM_BUFFERLENGTH, data.size(),
        CURLFORM_END);
}
Example #16
0
File: io.c Project: ThatisOK/c-sdk
Qiniu_Error Qiniu_Io_PutFile(
	Qiniu_Client* self, Qiniu_Io_PutRet* ret,
	const char* uptoken, const char* key, const char* localFile, Qiniu_Io_PutExtra* extra)
{
	Qiniu_Io_form form;
	Qiniu_Io_form_init(&form, uptoken, key, extra);

	curl_formadd(
		&form.formpost, &form.lastptr, CURLFORM_COPYNAME, "file", CURLFORM_FILE, localFile, CURLFORM_END);

	return Qiniu_Io_call(self, ret, form.formpost);
}
void ResourceHandleManager::setupPOST(ResourceHandle* job)
{
    ResourceHandleInternal* d = job->getInternal();

    curl_easy_setopt(d->m_handle, CURLOPT_POST, TRUE);

    job->request().httpBody()->flatten(d->m_postBytes);
    if (d->m_postBytes.size() != 0) {
        curl_easy_setopt(d->m_handle, CURLOPT_POSTFIELDSIZE, d->m_postBytes.size());
        curl_easy_setopt(d->m_handle, CURLOPT_POSTFIELDS, d->m_postBytes.data());
    }

    Vector<FormDataElement> elements = job->request().httpBody()->elements();
    size_t size = elements.size();
    struct curl_httppost* lastItem = 0;
    struct curl_httppost* post = 0;
    for (size_t i = 0; i < size; i++) {
        if (elements[i].m_type != FormDataElement::encodedFile)
            continue;
        CString cstring = elements[i].m_filename.utf8();
        ASSERT(!d->m_fileName);
        d->m_fileName = strdup(cstring.data());

        // Fill in the file upload field
        curl_formadd(&post, &lastItem, CURLFORM_COPYNAME, "sendfile", CURLFORM_FILE, d->m_fileName, CURLFORM_END);

        // Fill in the filename field
        curl_formadd(&post, &lastItem, CURLFORM_COPYNAME, "filename", CURLFORM_COPYCONTENTS, d->m_fileName, CURLFORM_END);

        // FIXME: We should not add a "submit" field for each file uploaded. Review this code.
        // Fill in the submit field too, even if this is rarely needed
        curl_formadd(&post, &lastItem, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "send", CURLFORM_END);

        // FIXME: should we support more than one file?
        break;
    }

    if (post)
        curl_easy_setopt(d->m_handle, CURLOPT_HTTPPOST, post);
}
Example #18
0
File: curl.c Project: NgoHuy/uim
static void *
uim_curl_post_internal(struct uim_curl_post_args *args)
{
  uim_lisp post_ = args->post;
  uim_lisp post_car_, post_cdr_;
  uim_lisp fetched_str_;
  const char *url = REFER_C_STR(args->url);
  CURL *curl;
  CURLcode res;
  struct curl_memory_struct chunk;
  struct curl_httppost* post_first = NULL;
  struct curl_httppost* post_last = NULL;

  curl = curl_easy_init();

  if(curl == NULL)
    return uim_scm_f();

  memset(&chunk, 0, sizeof(struct curl_memory_struct));

  curl_easy_setopt(curl, CURLOPT_URL, url);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, uim_curl_write_func);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

  for(post_cdr_ = post_;
      !NULLP(post_cdr_);
      post_cdr_ = CDR(post_cdr_)) {
      const char *name, *value;
      post_car_ = CAR(post_cdr_);

      name = REFER_C_STR(CAR(post_car_));
      value = REFER_C_STR(CDR(post_car_));

      curl_formadd(&post_first, &post_last,
		   CURLFORM_COPYNAME, name,
		   CURLFORM_COPYCONTENTS, value,
		   CURLFORM_END);
  }

  curl_easy_setopt(curl, CURLOPT_HTTPPOST, post_first);

  res = uim_curl_perform(curl);

  fetched_str_ = (chunk.str != NULL) ? MAKE_STR(chunk.str) : uim_scm_f();

  curl_easy_cleanup(curl);
  curl_formfree(post_first);
  curl_global_cleanup();
  free(chunk.str);

  return (void *)fetched_str_;
}
Example #19
0
int send_file_to_server(string path, string id) {

    int http_code;
    CURL *curl;
    CURLcode res;

    struct curl_httppost *formpost=NULL;
    struct curl_httppost *lastptr=NULL;
    struct curl_slist *headerlist=NULL;
    static const char buf[] = "Expect:";

    curl_global_init(CURL_GLOBAL_ALL);

    /* Fill in the file upload field */
    curl_formadd(&formpost,
                 &lastptr,
                 CURLFORM_COPYNAME, "sendfile",
                 CURLFORM_FILE, path.c_str(),
                 CURLFORM_END);


    curl = curl_easy_init();
    /* initialize custom header list (stating that Expect: 100-continue is not
       wanted */
    headerlist = curl_slist_append(headerlist, buf);
    if(curl) {
        /* what URL that receives this POST */
        curl_easy_setopt(curl, CURLOPT_URL, PHOTO_HOST);
//        if((argc == 2) && (!strcmp(argv[1], "noexpectheader")))
//            /* only disable 100-continue header if explicitly requested */
//            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
        curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);

        curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
        /* Check for errors */
        if(res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));

        /* always cleanup */
        curl_easy_cleanup(curl);

        /* then cleanup the formpost chain */
        curl_formfree(formpost);
        /* free slist */
        curl_slist_free_all (headerlist);
    }
    return http_code;
}
Example #20
0
File: io.c Project: ThatisOK/c-sdk
Qiniu_Error Qiniu_Io_PutBuffer(
	Qiniu_Client* self, Qiniu_Io_PutRet* ret,
	const char* uptoken, const char* key, const char* buf, size_t fsize, Qiniu_Io_PutExtra* extra)
{
	Qiniu_Io_form form;
	Qiniu_Io_form_init(&form, uptoken, key, extra);

	curl_formadd(
		&form.formpost, &form.lastptr, CURLFORM_COPYNAME, "file",
		CURLFORM_BUFFER, key, CURLFORM_BUFFERPTR, buf, CURLFORM_BUFFERLENGTH, fsize, CURLFORM_END);

	return Qiniu_Io_call(self, ret, form.formpost);
}
Example #21
0
/**
 * Submit a run
 * @param bott      Bott file for Run info
 * @return Submit status
 */
int SPOJJudger::submit(Bott * bott) {

  // prepare form for post
  struct curl_httppost * formpost = NULL;
  struct curl_httppost * lastptr = NULL;
  curl_formadd(&formpost, &lastptr,
               CURLFORM_COPYNAME, "submit",
               CURLFORM_COPYCONTENTS, "Send",
               CURLFORM_END);
  curl_formadd(&formpost, &lastptr,
               CURLFORM_COPYNAME, "problemcode",
               CURLFORM_COPYCONTENTS, bott->Getvid().c_str(),
               CURLFORM_END);
  curl_formadd(&formpost, &lastptr,
               CURLFORM_COPYNAME, "lang",
               CURLFORM_COPYCONTENTS,
                   convertLanguage(bott->Getlanguage()).c_str(),
               CURLFORM_END);
  curl_formadd(&formpost, &lastptr,
               CURLFORM_COPYNAME, "file",
               CURLFORM_COPYCONTENTS, bott->Getsrc().c_str(),
               CURLFORM_END);

  prepareCurl();
  curl_easy_setopt(curl, CURLOPT_URL, "http://www.spoj.com/submit/complete/");
  curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  performCurl();

  // check submit status
  string html = loadAllFromFile(tmpfilename);
  // cout << html << endl;
  if (html.find("in this language for this problem") != string::npos) {
    return SUBMIT_INVALID_LANGUAGE;
  }
  if (html.find("<form name=\"login\"  action=\"") != string::npos) {
    return SUBMIT_OTHER_ERROR;
  }
  return VirtualJudger::SUBMIT_NORMAL;
}
	void sendSticker(int chat_id, string sticker, int reply_to_message_id, string reply_markup)
	{
		string URL = BOTAPI + "sendSticker";
		// ============ convert string to char array =========== // 
		char c[200];
		strncpy(c, URL.c_str(), sizeof(c));
		c[sizeof(c) - 1] = 0;
		
		FILE *fp;
		CURL *curl = init(c, "sendSticker.json", fp);
		// ============ add fields =========== // 
		curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "chat_id", CURLFORM_COPYCONTENTS, to_string(chat_id).c_str(), CURLFORM_END);
		curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "sticker", CURLFORM_FILE, sticker.c_str(), CURLFORM_END);
		curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "reply_to_message_id", CURLFORM_COPYCONTENTS, to_string(reply_to_message_id).c_str(), CURLFORM_END);
		curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "reply_markup", CURLFORM_COPYCONTENTS, reply_markup.c_str(), CURLFORM_END);
		curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
		curl_easy_perform(curl);
	    curl_easy_cleanup(curl);
		formpost = NULL;
		lastptr = NULL;
		fclose(fp);
	}
Example #23
0
/* Returns 0 on success */
static CURLcode http_post_to_kerneloops_site(const char *url, const char *oopsdata)
{
    CURLcode ret;
    CURL *handle;
    struct curl_httppost *post = NULL;
    struct curl_httppost *last = NULL;
    struct curl_slist *headers = NULL;

    handle = curl_easy_init();
    if (!handle)
        error_msg_and_die("Can't create curl handle");

    headers = curl_slist_append(headers, "Accept: */*");
    headers = curl_slist_append(headers, "Expect:");

    curl_easy_setopt(handle, CURLOPT_URL, url);
    curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1L);
    curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);

    curl_formadd(&post, &last,
            CURLFORM_COPYNAME, "oopsdata",
            CURLFORM_COPYCONTENTS, oopsdata,
            CURLFORM_END);
    curl_formadd(&post, &last,
            CURLFORM_COPYNAME, "pass_on_allowed",
            CURLFORM_COPYCONTENTS, "yes",
            CURLFORM_END);

    curl_easy_setopt(handle, CURLOPT_HTTPPOST, post);
    curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, writefunction);

    ret = curl_easy_perform_with_proxy(handle, url);

    curl_formfree(post);
    curl_slist_free_all(headers);
    curl_easy_cleanup(handle);

    return ret;
}
Example #24
0
MemoryStruct FileUpload::Upload(const char* url, const char* filePath)
{
	struct curl_httppost *formpost=NULL;
	struct curl_httppost *lastptr=NULL;
	struct curl_slist *headerlist=NULL;
	static const char buf[] = "Expect:";

	CURL *curl;
	CURLcode res;
	struct stat file_info;
	struct MemoryStruct chunk; 
	chunk.memory=NULL;
    chunk.size = 0;

	curl_global_init(CURL_GLOBAL_ALL);	
		
	/* Fill in the file upload field */ 
	curl_formadd(&formpost,
				&lastptr,
				CURLFORM_COPYNAME, "sendfile",
				CURLFORM_FILE, filePath,
				CURLFORM_END);

	headerlist = curl_slist_append(headerlist, buf);

	curl = curl_easy_init();
	headerlist = curl_slist_append(headerlist, buf);

	if (curl)
	{		
		curl_easy_setopt(curl, CURLOPT_URL, url);		
		curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);		
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);	
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

		res = curl_easy_perform(curl);		
		
		curl_formfree(formpost);
		curl_slist_free_all (headerlist);
		curl_easy_cleanup(curl);

	}else{

		cout<<"Error loading module."<<endl;
	}

	curl_global_cleanup();

	return chunk;
}
Example #25
0
int cb_bz_login(cb_t cb)
{
	struct curl_httppost *formpost=NULL;
	struct curl_httppost *lastptr=NULL;
	//struct curl_slist *headerlist=NULL;

	char *url=strdup(cb->url.mem);
	CB_ENULL(url);

	int len;
	len = (cb->url.len + strlen(url_login) + 1);
	url = realloc(url, sizeof(char) * len);
	CB_ENULL(url);

	url = strcat(url, url_login);
	CB_ENULL(url);

	CB_CURLE(curl_easy_setopt(cb->curl, CURLOPT_URL, url));

	CB_BO(curl_formadd(&formpost,
		&lastptr,
		CURLFORM_COPYNAME, "Bugzilla_login",
		CURLFORM_COPYCONTENTS, cb->auth_user.mem,
		CURLFORM_END));

	CB_BO(curl_formadd(&formpost,
		&lastptr,
		CURLFORM_COPYNAME, "Bugzilla_password",
		CURLFORM_COPYCONTENTS, cb->auth_pass.mem,
		CURLFORM_END));

	CB_CURLE(curl_easy_setopt(cb->curl, CURLOPT_HTTPPOST, formpost));
	CB_BO(cb->curl_perform(cb));

	curl_formfree(formpost);

	return CB_SUCCESS;
}
Example #26
0
void Session::Impl::SetMultipart(const Multipart& multipart) {
    auto curl = curl_->handle;
    if (curl) {
        struct curl_httppost* formpost = NULL;
        struct curl_httppost* lastptr = NULL;

        for (auto& part : multipart.parts) {
            auto content_option = CURLFORM_PTRCONTENTS;
            if (part.is_file) {
                content_option = CURLFORM_FILE;
            }
            if (part.content_type.empty()) {
                curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, part.name.data(),
                             content_option, part.value.data(), CURLFORM_END);
            } else {
                curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, part.name.data(),
                             content_option, part.value.data(), CURLFORM_CONTENTTYPE,
                             part.content_type.data(), CURLFORM_END);
            }
        }
        curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
    }
}
Example #27
0
File: io.c Project: ThatisOK/c-sdk
static void Qiniu_Io_form_init(
	Qiniu_Io_form* self, const char* uptoken, const char* key, Qiniu_Io_PutExtra* extra)
{
	Qiniu_Io_PutExtraParam* param;
	struct curl_httppost* formpost = NULL;
	struct curl_httppost* lastptr = NULL;

	curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "token", CURLFORM_COPYCONTENTS, uptoken, CURLFORM_END);

	if (extra == NULL) {
		extra = &qiniu_defaultExtra;
	}
	if (key != NULL) {
		curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "key", CURLFORM_COPYCONTENTS, key, CURLFORM_END);
	}
	for (param = extra->params; param != NULL; param = param->next) {
		curl_formadd(
			&formpost, &lastptr, CURLFORM_COPYNAME, param->key, CURLFORM_COPYCONTENTS, param->value, CURLFORM_END);
	}

	self->formpost = formpost;
	self->lastptr = lastptr;
}
Example #28
0
telebot_error_e telebot_core_send_chat_action(telebot_core_h *handler, 
        char *chat_id, char *action)
{
    if (handler == NULL) {
        ERR("Handler is NULL");
        return TELEBOT_ERROR_INVALID_PARAMETER;
    }

    if (handler->token == NULL) {
        ERR("Token is NULL");
        return TELEBOT_ERROR_INVALID_PARAMETER;
    }

    struct curl_httppost *post = NULL;
    struct curl_httppost *last = NULL;

    curl_formadd(&post, &last, CURLFORM_COPYNAME, "chat_id", 
            CURLFORM_COPYCONTENTS, chat_id, CURLFORM_END);
    curl_formadd(&post, &last, CURLFORM_COPYNAME, "action", 
            CURLFORM_COPYCONTENTS, action, CURLFORM_END);

    return telebot_core_curl_perform(handler, TELEBOT_METHOD_SEND_CHATACTION, post);
}
Example #29
0
int wswcurl_formadd_raw(wswcurl_req *req, const char *field, void *data, size_t size )
{
	if (!req) return -1;
	if (!data) return -2;
	if (!size) return -3;

	// TODO: set the Content-type: to some other or just accept base64(URL) encoding here?
	curl_formadd(&req->post, &req->post_last,
					CURLFORM_COPYNAME, field,
					CURLFORM_COPYCONTENTS, data,
					CURLFORM_CONTENTSLENGTH, size,
					CURLFORM_END);
	return 0;
}
Example #30
0
int wswcurl_formadd(wswcurl_req *req, const char *field, const char *value, ...)
{
	va_list arg;
	char buf[1024];
	if (!req) return -1;
	if (!field) return -2;
	if (!value) return -3;

	va_start(arg, value);
	Q_vsnprintfz(buf, sizeof(buf), value, arg);
	va_end(arg);
	curl_formadd(&req->post, &req->post_last, CURLFORM_COPYNAME, field, CURLFORM_COPYCONTENTS, buf, CURLFORM_END);
	return 0;
}