doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lib506.c
Go to the documentation of this file.
1 #include "test.h"
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <errno.h>
5 
6 const char *HOSTHEADER = "Host: www.host.foo.com";
7 const char *JAR = "log/jar506";
8 #define THREADS 2
9 
10 void lock(CURL *handle, curl_lock_data data, curl_lock_access access,
11  void *useptr );
12 void unlock(CURL *handle, curl_lock_data data, void *useptr );
13 struct curl_slist *sethost(struct curl_slist *headers);
14 void *fire(void *ptr);
15 char *suburl(char *base, int i);
16 
17 /* struct containing data of a thread */
18 struct Tdata {
20  char *url;
21 };
22 
23 struct userdata {
24  char *text;
25  int counter;
26 };
27 
28 /* lock callback */
30  void *useptr )
31 {
32  const char *what;
33  struct userdata *user = (struct userdata *)useptr;
34 
35  (void)handle;
36  (void)access;
37 
38  switch ( data ) {
40  what = "share";
41  break;
42  case CURL_LOCK_DATA_DNS:
43  what = "dns";
44  break;
46  what = "cookie";
47  break;
48  default:
49  fprintf(stderr, "lock: no such data: %d\n",data);
50  return;
51  }
52  printf("lock: %-6s <%s>: %d\n", what, user->text, user->counter);
53  user->counter++;
54 }
55 
56 /* unlock callback */
57 void unlock(CURL *handle, curl_lock_data data, void *useptr )
58 {
59  const char *what;
60  struct userdata *user = (struct userdata *)useptr;
61  (void)handle;
62  switch ( data ) {
64  what = "share";
65  break;
66  case CURL_LOCK_DATA_DNS:
67  what = "dns";
68  break;
70  what = "cookie";
71  break;
72  default:
73  fprintf(stderr, "unlock: no such data: %d\n",data);
74  return;
75  }
76  printf("unlock: %-6s <%s>: %d\n", what, user->text, user->counter);
77  user->counter++;
78 }
79 
80 
81 /* build host entry */
82 struct curl_slist *sethost(struct curl_slist *headers)
83 {
84  (void)headers;
86 }
87 
88 
89 /* the dummy thread function */
90 void *fire(void *ptr)
91 {
92  CURLcode code;
93  struct curl_slist *headers;
94  struct Tdata *tdata = (struct Tdata*)ptr;
95  CURL *curl = curl_easy_init();
96  int i=0;
97 
98  headers = sethost(NULL);
99  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
100  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, (void*)headers);
101  curl_easy_setopt(curl, CURLOPT_URL, (void*)tdata->url);
102  printf( "CURLOPT_SHARE\n" );
103  curl_easy_setopt(curl, CURLOPT_SHARE, (void*)tdata->share);
104 
105  printf( "PERFORM\n" );
106  code = curl_easy_perform(curl);
107  if( code != CURLE_OK ) {
108  fprintf(stderr, "perform url '%s' repeat %d failed, curlcode %d\n",
109  tdata->url, i, (int)code);
110  }
111 
112  printf( "CLEANUP\n" );
113  curl_easy_cleanup(curl);
114  curl_slist_free_all(headers);
115 
116  return NULL;
117 }
118 
119 
120 /* build request url */
121 char *suburl(char *base, int i)
122 {
123  int len = strlen(base);
124  char *url = (char *)malloc(len+5);
125  if (!url) {
126  abort();
127  }
128  strcpy(url, base);
129  strcat(url, "0000");
130  url[len+3] = 48+i;
131  return url;
132 }
133 
134 
135 /* test function */
136 int test(char *URL)
137 {
138  int res;
139  CURLSHcode scode;
140  char *url;
141  struct Tdata tdata;
142  CURL *curl;
143  CURLSH *share;
144  struct curl_slist *headers;
145  int i;
146  struct userdata user;
147 
148  user.text = (char *)"Pigs in space";
149  user.counter = 0;
150 
151  printf( "GLOBAL_INIT\n" );
153 
154  /* prepare share */
155  printf( "SHARE_INIT\n" );
156  share = curl_share_init();
159  curl_share_setopt( share, CURLSHOPT_USERDATA, &user);
160  printf( "CURL_LOCK_DATA_COOKIE\n" );
162  printf( "CURL_LOCK_DATA_DNS\n" );
164 
165  res = 0;
166 
167  /* start treads */
168  for (i=1; i<=THREADS; i++ ) {
169 
170  /* set thread data */
171  tdata.url = suburl( URL, i ); /* must be freed */
172  tdata.share = share;
173 
174  /* simulate thread, direct call of "thread" function */
175  printf( "*** run %d\n",i );
176  fire( &tdata );
177 
178  free( tdata.url );
179 
180  }
181 
182 
183  /* fetch a another one and save cookies */
184  printf( "*** run %d\n", i );
185  curl = curl_easy_init();
186 
187  url = suburl( URL, i );
188  headers = sethost( NULL );
189  curl_easy_setopt( curl, CURLOPT_HTTPHEADER, (void*)headers );
190  curl_easy_setopt( curl, CURLOPT_URL, url );
191  printf( "CURLOPT_SHARE\n" );
192  curl_easy_setopt( curl, CURLOPT_SHARE, share );
193  printf( "CURLOPT_COOKIEJAR\n" );
194  curl_easy_setopt( curl, CURLOPT_COOKIEJAR, JAR );
195 
196  printf( "PERFORM\n" );
197  curl_easy_perform( curl );
198 
199  /* try to free share, expect to fail because share is in use*/
200  printf( "try SHARE_CLEANUP...\n" );
201  scode = curl_share_cleanup( share );
202  if ( scode==CURLSHE_OK )
203  {
204  fprintf(stderr, "curl_share_cleanup succeed but error expected\n");
205  share = NULL;
206  } else {
207  printf( "SHARE_CLEANUP failed, correct\n" );
208  }
209 
210  /* clean up last handle */
211  printf( "CLEANUP\n" );
212  curl_easy_cleanup( curl );
213  curl_slist_free_all( headers );
214  free(url);
215 
216 
217  /* free share */
218  printf( "SHARE_CLEANUP\n" );
219  scode = curl_share_cleanup( share );
220  if ( scode!=CURLSHE_OK )
221  {
222  fprintf(stderr, "curl_share_cleanup failed, code errno %d\n", scode);
223  }
224 
225  printf( "GLOBAL_CLEANUP\n" );
227 
228  return res;
229 }
230 
CURLcode curl_global_init(long flags)
Globally initializes cURL given a bitwise set of the different features to initialize.
Definition: easy.c:147
void lock(CURL *handle, curl_lock_data data, curl_lock_access access, void *useptr)
Definition: lib506.c:29
char * suburl(char *base, int i)
Definition: lib506.c:121
char * url
Definition: lib506.c:20
void * fire(void *ptr)
Definition: lib506.c:90
CURLcode curl_easy_perform(CURL *curl)
Definition: easy.c:260
CURLcode
Definition: curl.h:209
GLenum GLsizei len
Definition: glext.h:3472
CURLcode curl_easy_setopt(CURL *curl, CURLoption option,...)
Definition: easy.c:217
struct curl_slist * sethost(struct curl_slist *headers)
Definition: lib506.c:82
int i
Definition: process.py:33
CURLSHcode curl_share_setopt(CURLSH *, CURLSHoption option,...)
Definition: share.c:51
CURLSHcode curl_share_cleanup(CURLSH *)
Definition: share.c:151
#define THREADS
Definition: lib506.c:8
const char * HOSTHEADER
Definition: lib506.c:6
void curl_global_cleanup(void)
Globally cleanup cURL, uses the value of "init_flags" to determine what needs to be cleaned up and ...
Definition: easy.c:174
#define NULL
Definition: Lib.h:88
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:2853
CURLSHcode
Definition: curl.h:1195
void CURLSH
Definition: curl.h:1193
const char * JAR
Definition: lib506.c:7
curl_lock_access
Definition: curl.h:1178
Definition: curl.h:210
int test(char *URL)
Definition: lib506.c:136
Definition: lib506.c:18
struct curl_slist * curl_slist_append(struct curl_slist *, const char *)
Definition: sendf.c:82
CURLSH * share
Definition: lib506.c:19
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
int counter
Definition: lib506.c:25
curl_lock_data
Definition: curl.h:1163
void CURL
Definition: types.h:25
void curl_slist_free_all(struct curl_slist *)
Definition: sendf.c:108
GLuint res
Definition: glext.h:5385
void unlock(CURL *handle, curl_lock_data data, void *useptr)
Definition: lib506.c:57
#define CURL_GLOBAL_ALL
Definition: curl.h:1153
void curl_easy_cleanup(CURL *curl)
Definition: easy.c:288
char * text
Definition: lib506.c:24
CURL * curl_easy_init(void)
Definition: easy.c:195
CURLSH * curl_share_init(void)
Definition: share.c:38