1 pjaol 1.1 #include "curl/curl.h"
2 #include <stdio.h>
3 #include "ns.h"
4 #include <tcl.h>
5
6
7 static curl_version_info_data *curlinfo;
8 char *version = NULL;
9
10 char * Version();
11 static int curlRead( void *ptr, size_t size, size_t nmemb, void *stream);
12 int ns_proxy_init (Tcl_Interp *interp, void *context);
13 int curlGet (ClientData arg, Tcl_Interp *interp, int argc, char **argv);
14 int curlVersion (ClientData arg, Tcl_Interp *interp, int argc, char **argv);
15
16 typedef struct clientData {
17 char *data; //holder for content
18 size_t size; // need holder for realloc
19 } clientData;
20
21
22 pjaol 1.1 int Ns_ModuleVersion = 1;
23
24 int
25 Ns_ModuleInit (char * server, char *argv[]) {
26
27 return (Ns_TclInitInterps(server,ns_proxy_init,NULL));
28 }
29
30
31 int
32 ns_proxy_init (Tcl_Interp *interp, void *context) {
33
34 Tcl_CreateCommand(interp, "ns_libcurl", curlGet, NULL, NULL);
35 Tcl_CreateCommand(interp, "ns_libcurl_version", curlVersion, NULL, NULL);
36 return NS_OK;
37 }
38
39 int
40 curlVersion (ClientData arg, Tcl_Interp *interp, int argc, char **argv) {
41
42 if (version == NULL) {
43 pjaol 1.1 curl_global_init(CURL_GLOBAL_ALL);
44 curlinfo = curl_version_info(CURLVERSION_NOW);
45 version = (char *) curlinfo->version;
46 curl_global_cleanup();
47 }
48
49 Tcl_AppendResult(interp, version, NULL);
50 return TCL_OK;
51
52 }
53
54 int
55 curlGet (ClientData arg, Tcl_Interp *interp, int argc, char **argv) {
56
57 if (argc < 1) {
58 Tcl_AppendResult(interp, "wrong # args: should be ns_libcurl url", NULL);
59 return TCL_ERROR;
60 }
61
62 CURL *curlhandle = NULL;
63
64 pjaol 1.1 struct clientData cd;
65 cd.data = NULL;
66 cd.size = 0;
67
68 curl_global_init(CURL_GLOBAL_ALL);
69
70 curlhandle = curl_easy_init();
71
72 curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION , curlRead); //overwrite conn->set.fwrite function
73 curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, &cd); //so now void * stream is set to &cd
74
75 curl_easy_setopt(curlhandle, CURLOPT_URL, argv[1]);
76 curl_easy_perform(curlhandle);
77 curl_easy_cleanup(curlhandle);
78
79 Tcl_AppendResult(interp,cd.data,NULL);
80
81 free(cd.data);
82
83 memset(&cd, 0, sizeof( cd));
84 //curl_global_cleanup();
85 pjaol 1.1
86 return TCL_OK;
87 }
88
89
90 static int
91 curlRead ( void *ptr, size_t size, size_t nmemb, void *stream) {
92
93 struct clientData * this_cd = (struct clientData *) stream;
94
95 size_t realsize = size * nmemb;
96
97 if (this_cd->data == NULL) {
98 this_cd->data = malloc(realsize +1);
99 } else {
100 this_cd->data = realloc(this_cd->data, (realsize + this_cd->size +1));
101 }
102
103 if(this_cd->data == NULL) {
104 perror("curlRead: cannot allocate memory, insert 50c into meter please");
105 return 0;
106 pjaol 1.1 }
107
108 memcpy(&(this_cd->data[this_cd->size]), (char *) ptr, realsize);
109
110 this_cd->size += realsize;
111 this_cd->data[this_cd->size] = 0;
112
113 return realsize;
114 }
115
116
117 int
118 Nslibcurl_Init (Tcl_Interp *interp) {
119
120 ns_proxy_init(interp, NULL);
121 return TCL_OK;
122 }
123
|