doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
post-callback.c
Go to the documentation of this file.
1 /*****************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * $Id: post-callback.c,v 1.3 2003/12/08 14:14:26 bagder Exp $
9  *
10  * An example source code that issues a HTTP POST and we provide the actual
11  * data through a read callback.
12  *
13  */
14 #include <stdio.h>
15 #include <string.h>
16 #include <curl/curl.h>
17 
18 char data[]="this is what we post to the silly web server";
19 
20 struct WriteThis {
21  char *readptr;
22  int sizeleft;
23 };
24 
25 size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
26 {
27  struct WriteThis *pooh = (struct WriteThis *)userp;
28 
29  if(size*nmemb < 1)
30  return 0;
31 
32  if(pooh->sizeleft) {
33  *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
34  pooh->readptr++; /* advance pointer */
35  pooh->sizeleft--; /* less data left */
36  return 1; /* we return 1 byte at a time! */
37  }
38 
39  return -1; /* no more data left to deliver */
40 }
41 
42 int main(void)
43 {
44  CURL *curl;
45  CURLcode res;
46 
47  struct WriteThis pooh;
48 
49  pooh.readptr = data;
50  pooh.sizeleft = strlen(data);
51 
52  curl = curl_easy_init();
53  if(curl) {
54  /* First set the URL that is about to receive our POST. */
55  curl_easy_setopt(curl, CURLOPT_URL,
56  "http://receivingsite.com.pooh/index.cgi");
57  /* Now specify we want to POST data */
58  curl_easy_setopt(curl, CURLOPT_POST, TRUE);
59 
60  /* Set the expected POST size */
61  curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, pooh.sizeleft);
62 
63  /* we want to use our own read function */
64  curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
65 
66  /* pointer to pass to our read function */
67  curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
68 
69  /* get verbose debug output please */
70  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
71 
72  /* Perform the request, res will get the return code */
73  res = curl_easy_perform(curl);
74 
75  /* always cleanup */
76  curl_easy_cleanup(curl);
77  }
78  return 0;
79 }
CURLcode curl_easy_perform(CURL *curl)
Definition: easy.c:260
CURLcode
Definition: curl.h:209
int main(void)
Definition: post-callback.c:42
char * readptr
Definition: post-callback.c:21
CURLcode curl_easy_setopt(CURL *curl, CURLoption option,...)
Definition: easy.c:217
char data[]
Definition: post-callback.c:18
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:2853
GLsizeiptr size
Definition: glext.h:3112
void CURL
Definition: types.h:25
GLuint res
Definition: glext.h:5385
#define TRUE
Definition: mprintf.c:69
size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
Definition: post-callback.c:25
#define CURLOPT_READDATA
Definition: curl.h:806
void curl_easy_cleanup(CURL *curl)
Definition: easy.c:288
CURL * curl_easy_init(void)
Definition: easy.c:195