11 .url_ready_func url_ready;
12 .entity url_ready_pass;
14 #define MIN_URL_ID 128
16 entity url_fromid[NUM_URL_ID];
18 float url_URI_Get_Callback(float id, float status, string data)
29 if(e.url_rbuf >= 0 || e.url_wbuf >= 0)
31 print(sprintf("WARNING: handle %d (%s) has already received data?!?\n", id + NUM_URL_ID, e.url_url));
35 // whatever happens, we will remove the URL from the list of IDs
36 url_fromid[id] = world;
42 n = tokenizebyseparator(data, "\n");
43 e.url_rbuf = buf_create();
47 print("buf_create: out of memory\n");
48 e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
52 for(i = 0; i < n; ++i)
53 bufstr_set(e.url_rbuf, i, argv(i));
54 e.url_ready(e, e.url_ready_pass, URL_READY_CANREAD);
60 e.url_ready(e, e.url_ready_pass, -status);
67 void url_fopen(string url, float mode, url_ready_func rdy, entity pass)
71 if(strstrofs(url, "://", -1))
77 // collect data to a stringbuffer for a POST request
78 // attempts to close will result in a reading handle
80 e.classname = "url_fopen_file";
81 e.url_url = strzone(url);
83 e.url_wbuf = buf_create();
86 print("buf_create: out of memory\n");
87 rdy(e, pass, URL_READY_ERROR);
94 rdy(e, pass, URL_READY_CANWRITE);
99 for(i = 0; i < NUM_URL_ID; ++i)
100 if(url_fromid[i] == world)
104 rdy(world, pass, URL_READY_ERROR);
109 e.classname = "url_fopen_file";
110 e.url_url = strzone(url);
114 if(!uri_get(url, i + MIN_URL_ID))
116 rdy(e, pass, URL_READY_ERROR);
117 strunzone(e.url_url);
122 e.url_ready_pass = pass;
131 fh = fopen(url, mode);
134 rdy(world, pass, URL_READY_ERROR);
140 e.classname = "url_fopen_file";
142 if(mode == FILE_READ)
143 rdy(e, pass, URL_READY_CANREAD);
145 rdy(e, pass, URL_READY_CANWRITE);
150 void url_fclose(entity e, url_ready_func rdy, entity pass)
158 for(i = 0; i < NUM_URL_ID; ++i)
159 if(url_fromid[i] == world)
164 rdy(e, pass, URL_READY_ERROR);
165 strunzone(e.url_url);
170 if(!uri_postbuf(e.url_url, e.url_id + MIN_URL_ID, "text/plain", "\n", e.url_wbuf))
173 rdy(e, pass, URL_READY_ERROR);
174 strunzone(e.url_url);
182 e.url_ready_pass = pass;
188 // we have READ all data
189 rdy(e, pass, URL_READY_CLOSED);
191 strunzone(e.url_url);
199 rdy(e, pass, URL_READY_CLOSED); // closing creates no reading handle
205 string url_fgets(entity e)
211 s = bufstr_get(e.url_rbuf, e.url_rbufpos);
218 return fgets(e.url_fh);
223 void url_fputs(entity e, string s)
228 bufstr_set(e.url_wbuf, e.url_wbufpos, s);