19784
|
1 /**
|
|
2 * @file soap.c
|
|
3 * SOAP connection related process
|
|
4 * Author
|
|
5 * MaYuan<mayuan2006@gmail.com>
|
|
6 * gaim
|
|
7 *
|
|
8 * Gaim is the legal property of its developers, whose names are too numerous
|
|
9 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
10 * source distribution.
|
|
11 *
|
|
12 * This program is free software; you can redistribute it and/or modify
|
|
13 * it under the terms of the GNU General Public License as published by
|
|
14 * the Free Software Foundation; either version 2 of the License, or
|
|
15 * (at your option) any later version.
|
|
16 *
|
|
17 * This program is distributed in the hope that it will be useful,
|
|
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
20 * GNU General Public License for more details.
|
|
21 *
|
|
22 * You should have received a copy of the GNU General Public License
|
|
23 * along with this program; if not, write to the Free Software
|
|
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
25 */
|
|
26 #include "msn.h"
|
|
27 #include "soap.h"
|
|
28
|
19785
|
29 //msn_soap_new(MsnSession *session,gpointer data,int sslconn)
|
19784
|
30 /*new a soap connection*/
|
|
31 MsnSoapConn *
|
19785
|
32 msn_soap_new(MsnSession *session,gpointer data,int sslconn)
|
19784
|
33 {
|
|
34 MsnSoapConn *soapconn;
|
|
35
|
|
36 soapconn = g_new0(MsnSoapConn, 1);
|
|
37 soapconn->session = session;
|
19785
|
38 soapconn->parent = data;
|
|
39 soapconn->ssl_conn = sslconn;
|
|
40
|
|
41 soapconn->gsc = NULL;
|
19784
|
42 soapconn->input_handler = -1;
|
|
43 soapconn->output_handler = -1;
|
19804
|
44
|
|
45 soapconn->soap_queue = g_queue_new();
|
19784
|
46 return soapconn;
|
|
47 }
|
|
48
|
|
49 /*ssl soap connect callback*/
|
|
50 void
|
|
51 msn_soap_connect_cb(gpointer data, GaimSslConnection *gsc,
|
|
52 GaimInputCondition cond)
|
|
53 {
|
|
54 MsnSoapConn * soapconn;
|
|
55 MsnSession *session;
|
|
56
|
|
57 gaim_debug_info("MaYuan","Soap connection connected!\n");
|
|
58 soapconn = data;
|
|
59 g_return_if_fail(soapconn != NULL);
|
|
60
|
|
61 session = soapconn->session;
|
|
62 g_return_if_fail(session != NULL);
|
|
63
|
|
64 soapconn->gsc = gsc;
|
|
65
|
|
66 /*connection callback*/
|
|
67 if(soapconn->connect_cb != NULL){
|
|
68 soapconn->connect_cb(data,gsc,cond);
|
|
69 }
|
19804
|
70
|
|
71 /*we do the SOAP request here*/
|
|
72 msn_soap_post_head_request(soapconn);
|
19784
|
73 }
|
|
74
|
|
75 /*ssl soap error callback*/
|
|
76 static void
|
|
77 msn_soap_error_cb(GaimSslConnection *gsc, GaimSslErrorType error, void *data)
|
|
78 {
|
|
79 MsnSoapConn * soapconn = data;
|
|
80 g_return_if_fail(data != NULL);
|
|
81 gaim_debug_info("MaYuan","Soap connection error!\n");
|
|
82 /*error callback*/
|
|
83 if(soapconn->error_cb != NULL){
|
|
84 soapconn->error_cb(gsc,error,data);
|
|
85 }
|
|
86 }
|
|
87
|
|
88 /*init the soap connection*/
|
|
89 void
|
|
90 msn_soap_init(MsnSoapConn *soapconn,char * host,int ssl,
|
|
91 GaimSslInputFunction connect_cb,
|
|
92 GaimSslErrorFunction error_cb)
|
|
93 {
|
|
94 soapconn->login_host = g_strdup(host);
|
|
95 soapconn->ssl_conn = ssl;
|
|
96 soapconn->connect_cb = connect_cb;
|
|
97 soapconn->error_cb = error_cb;
|
19804
|
98 }
|
|
99
|
|
100 void
|
|
101 msn_soap_connect(MsnSoapConn *soapconn)
|
|
102 {
|
19784
|
103 if(soapconn->ssl_conn){
|
|
104 gaim_ssl_connect(soapconn->session->account, soapconn->login_host,
|
|
105 GAIM_SSL_DEFAULT_PORT, msn_soap_connect_cb, msn_soap_error_cb,
|
|
106 soapconn);
|
|
107 }else{
|
|
108 }
|
|
109 }
|
|
110
|
19804
|
111 void
|
|
112 msn_soap_close(MsnSoapConn *soapconn)
|
|
113 {
|
|
114 if(soapconn->ssl_conn){
|
|
115 if(soapconn->gsc != NULL){
|
|
116 gaim_ssl_close(soapconn->gsc);
|
|
117 soapconn->gsc = NULL;
|
|
118 }
|
|
119 }else{
|
|
120 }
|
|
121 }
|
|
122
|
19784
|
123 /*destroy the soap connection*/
|
|
124 void
|
|
125 msn_soap_destroy(MsnSoapConn *soapconn)
|
|
126 {
|
19804
|
127 MsnSoapReq *request;
|
|
128
|
19791
|
129 if(soapconn->login_host)
|
|
130 g_free(soapconn->login_host);
|
|
131
|
|
132 if(soapconn->login_path)
|
|
133 g_free(soapconn->login_path);
|
19784
|
134
|
|
135 /*remove the write handler*/
|
|
136 if (soapconn->output_handler > 0){
|
|
137 gaim_input_remove(soapconn->output_handler);
|
|
138 }
|
|
139 /*remove the read handler*/
|
|
140 if (soapconn->input_handler > 0){
|
|
141 gaim_input_remove(soapconn->input_handler);
|
|
142 }
|
|
143 msn_soap_free_read_buf(soapconn);
|
|
144 msn_soap_free_write_buf(soapconn);
|
|
145
|
|
146 /*close ssl connection*/
|
19804
|
147 msn_soap_close(soapconn);
|
|
148
|
|
149 while ((request = g_queue_pop_head(soapconn->soap_queue)) != NULL){
|
|
150 msn_soap_request_free(request);
|
19788
|
151 }
|
19804
|
152 g_queue_free(soapconn->soap_queue);
|
19784
|
153 g_free(soapconn);
|
|
154 }
|
|
155
|
19785
|
156 /*check the soap is connected?
|
|
157 * if connected return 0
|
|
158 */
|
|
159 int
|
|
160 msn_soap_connected(MsnSoapConn *soapconn)
|
|
161 {
|
|
162 if(soapconn->ssl_conn){
|
|
163 return (soapconn->gsc == NULL? -1 : 0);
|
|
164 }
|
|
165 return(soapconn->fd>0? 0 : -1);
|
|
166 }
|
|
167
|
19784
|
168 /*read and append the content to the buffer*/
|
|
169 static gssize
|
|
170 msn_soap_read(MsnSoapConn *soapconn)
|
|
171 {
|
|
172 gssize len;
|
|
173 gssize total_len = 0;
|
19796
|
174 char temp_buf[MSN_SOAP_READ_BUFF_SIZE];
|
19784
|
175
|
|
176 if(soapconn->ssl_conn){
|
19796
|
177 len = gaim_ssl_read(soapconn->gsc, temp_buf,MSN_SOAP_READ_BUFF_SIZE);
|
19784
|
178 }else{
|
19796
|
179 len = read(soapconn->fd, temp_buf,MSN_SOAP_READ_BUFF_SIZE);
|
19784
|
180 }
|
|
181 if(len >0){
|
|
182 total_len += len;
|
|
183 soapconn->read_buf = g_realloc(soapconn->read_buf,
|
|
184 soapconn->read_len + len + 1);
|
|
185 // strncpy(soapconn->read_buf + soapconn->read_len, temp_buf, len);
|
|
186 memcpy(soapconn->read_buf + soapconn->read_len, temp_buf, len);
|
|
187 soapconn->read_len += len;
|
|
188 soapconn->read_buf[soapconn->read_len] = '\0';
|
|
189 }
|
19796
|
190 gaim_debug_info("MaYuan","++soap ssl read:{%d}\n",total_len);
|
19784
|
191 // gaim_debug_info("MaYuan","nexus ssl read:{%s}\n",soapconn->read_buf);
|
|
192 return total_len;
|
|
193 }
|
|
194
|
|
195 /*read the whole SOAP server response*/
|
|
196 void
|
|
197 msn_soap_read_cb(gpointer data, gint source, GaimInputCondition cond)
|
|
198 {
|
|
199 MsnSoapConn *soapconn = data;
|
|
200 MsnSession *session;
|
|
201 int len;
|
|
202 char * body_start,*body_len;
|
|
203 char *length_start,*length_end;
|
|
204
|
19785
|
205 // gaim_debug_misc("MaYuan", "soap read cb\n");
|
19784
|
206 session = soapconn->session;
|
|
207 g_return_if_fail(session != NULL);
|
|
208
|
|
209 if (soapconn->input_handler == -1){
|
|
210 soapconn->input_handler = gaim_input_add(soapconn->gsc->fd,
|
|
211 GAIM_INPUT_READ, msn_soap_read_cb, soapconn);
|
|
212 }
|
|
213
|
|
214 /*read the request header*/
|
|
215 len = msn_soap_read(soapconn);
|
|
216 if (len < 0 && errno == EAGAIN){
|
|
217 return;
|
|
218 }else if (len < 0) {
|
|
219 gaim_debug_error("msn", "read Error!len:%d\n",len);
|
|
220 gaim_input_remove(soapconn->input_handler);
|
|
221 soapconn->input_handler = -1;
|
|
222 g_free(soapconn->read_buf);
|
|
223 soapconn->read_buf = NULL;
|
|
224 soapconn->read_len = 0;
|
|
225 /* TODO: error handling */
|
|
226 return;
|
|
227 }
|
|
228
|
|
229 if(soapconn->read_buf == NULL){
|
|
230 return;
|
|
231 }
|
|
232
|
19788
|
233 if (strstr(soapconn->read_buf, "HTTP/1.1 302") != NULL)
|
|
234 {
|
|
235 /* Redirect. */
|
|
236 char *location, *c;
|
|
237
|
19796
|
238 gaim_debug_error("MaYuan", "soap redirect\n");
|
19788
|
239 location = strstr(soapconn->read_buf, "Location: ");
|
|
240 if (location == NULL)
|
|
241 {
|
|
242 msn_soap_free_read_buf(soapconn);
|
|
243
|
|
244 return;
|
|
245 }
|
|
246 location = strchr(location, ' ') + 1;
|
|
247
|
|
248 if ((c = strchr(location, '\r')) != NULL)
|
|
249 *c = '\0';
|
|
250
|
|
251 /* Skip the http:// */
|
|
252 if ((c = strchr(location, '/')) != NULL)
|
|
253 location = c + 2;
|
|
254
|
|
255 if ((c = strchr(location, '/')) != NULL)
|
|
256 {
|
|
257 g_free(soapconn->login_path);
|
|
258 soapconn->login_path = g_strdup(c);
|
|
259
|
|
260 *c = '\0';
|
|
261 }
|
|
262
|
|
263 g_free(soapconn->login_host);
|
|
264 soapconn->login_host = g_strdup(location);
|
|
265
|
|
266 gaim_ssl_connect(session->account, soapconn->login_host,
|
|
267 GAIM_SSL_DEFAULT_PORT, msn_soap_connect_cb,
|
|
268 msn_soap_error_cb, soapconn);
|
19784
|
269 }
|
19788
|
270 else if (strstr(soapconn->read_buf, "HTTP/1.1 401 Unauthorized") != NULL)
|
|
271 {
|
|
272 const char *error;
|
19784
|
273
|
19796
|
274 gaim_debug_error("MaYuan", "soap 401\n");
|
19788
|
275 if ((error = strstr(soapconn->read_buf, "WWW-Authenticate")) != NULL)
|
|
276 {
|
|
277 if ((error = strstr(error, "cbtxt=")) != NULL)
|
|
278 {
|
|
279 const char *c;
|
|
280 char *temp;
|
|
281
|
|
282 error += strlen("cbtxt=");
|
|
283
|
|
284 if ((c = strchr(error, '\n')) == NULL)
|
|
285 c = error + strlen(error);
|
|
286
|
|
287 temp = g_strndup(error, c - error);
|
|
288 error = gaim_url_decode(temp);
|
|
289 g_free(temp);
|
|
290 }
|
|
291 }
|
19784
|
292
|
19788
|
293 msn_session_set_error(session, MSN_ERROR_SERV_UNAVAILABLE, error);
|
|
294 }
|
|
295 else if (strstr(soapconn->read_buf, "HTTP/1.1 200 OK"))
|
|
296 {
|
|
297 /*OK! process the SOAP body*/
|
|
298 body_start = (char *)g_strstr_len(soapconn->read_buf, soapconn->read_len,"\r\n\r\n");
|
|
299 if(!body_start){
|
|
300 return;
|
|
301 }
|
|
302 body_start += 4;
|
|
303
|
|
304 // gaim_debug_misc("msn", "Soap Read: {%s}\n", soapconn->read_buf);
|
19784
|
305
|
19788
|
306 /* we read the content-length*/
|
|
307 length_start = strstr(soapconn->read_buf, "Content-Length: ");
|
|
308 length_start += strlen("Content-Length: ");
|
|
309 length_end = strstr(length_start, "\r\n");
|
|
310 body_len = g_strndup(length_start,length_end - length_start);
|
19784
|
311
|
19788
|
312 /*setup the conn body */
|
|
313 soapconn->body = body_start;
|
|
314 soapconn->body_len = atoi(body_len);
|
19796
|
315 gaim_debug_misc("MaYuan","SOAP Read length :%d,body len:%d\n",soapconn->read_len,soapconn->body_len);
|
19788
|
316
|
19796
|
317 if(soapconn->read_len < body_start - soapconn->read_buf + soapconn->body_len){
|
19804
|
318 return;
|
19788
|
319 }
|
|
320 g_free(body_len);
|
19784
|
321
|
19785
|
322 #if 1
|
19788
|
323 /*remove the read handler*/
|
|
324 gaim_input_remove(soapconn->input_handler);
|
|
325 soapconn->input_handler = -1;
|
19785
|
326 #endif
|
|
327
|
19788
|
328 /*call the read callback*/
|
|
329 if(soapconn->read_cb != NULL){
|
19804
|
330 soapconn->read_cb(soapconn,source,0);
|
19788
|
331 }
|
19804
|
332
|
|
333 /*Process the next queued SOAP request*/
|
|
334 msn_soap_post_head_request(soapconn);
|
|
335
|
19784
|
336 #if 0
|
|
337 /*clear the read buffer*/
|
|
338 msn_soap_free_read_buf(soapconn);
|
|
339
|
|
340 /*remove the read handler*/
|
|
341 gaim_input_remove(soapconn->input_handler);
|
|
342 soapconn->input_handler = -1;
|
19788
|
343 // gaim_ssl_close(soapconn->gsc);
|
|
344 // soapconn->gsc = NULL;
|
19784
|
345 #endif
|
19788
|
346 }
|
19796
|
347 return;
|
19784
|
348 }
|
|
349
|
|
350 void
|
|
351 msn_soap_free_read_buf(MsnSoapConn *soapconn)
|
|
352 {
|
|
353 if(soapconn->read_buf){
|
|
354 g_free(soapconn->read_buf);
|
|
355 }
|
|
356 soapconn->read_buf = NULL;
|
|
357 soapconn->read_len = 0;
|
|
358 }
|
|
359
|
|
360 void
|
|
361 msn_soap_free_write_buf(MsnSoapConn *soapconn)
|
|
362 {
|
|
363 if(soapconn->write_buf){
|
|
364 g_free(soapconn->write_buf);
|
|
365 }
|
|
366 soapconn->write_buf = NULL;
|
|
367 soapconn->written_len = 0;
|
|
368 }
|
|
369
|
|
370 /*Soap write process func*/
|
|
371 static void
|
|
372 msn_soap_write_cb(gpointer data, gint source, GaimInputCondition cond)
|
|
373 {
|
|
374 MsnSoapConn *soapconn = data;
|
|
375 int len, total_len;
|
|
376
|
19796
|
377 g_return_if_fail(soapconn != NULL);
|
|
378 if(soapconn->write_buf == NULL){
|
|
379 gaim_debug_error("MaYuan","soap buffer is NULL\n");
|
|
380 gaim_input_remove(soapconn->output_handler);
|
|
381 soapconn->output_handler = -1;
|
|
382 return;
|
|
383 }
|
19784
|
384 total_len = strlen(soapconn->write_buf);
|
|
385
|
|
386 /*
|
|
387 * write the content to SSL server,
|
|
388 */
|
|
389 len = gaim_ssl_write(soapconn->gsc,
|
|
390 soapconn->write_buf + soapconn->written_len,
|
|
391 total_len - soapconn->written_len);
|
|
392
|
|
393 if (len < 0 && errno == EAGAIN)
|
|
394 return;
|
|
395 else if (len <= 0){
|
|
396 /*SSL write error!*/
|
|
397 gaim_input_remove(soapconn->output_handler);
|
|
398 soapconn->output_handler = -1;
|
|
399 /* TODO: notify of the error */
|
|
400 return;
|
|
401 }
|
|
402 soapconn->written_len += len;
|
|
403
|
|
404 if (soapconn->written_len < total_len)
|
|
405 return;
|
|
406
|
|
407 gaim_input_remove(soapconn->output_handler);
|
|
408 soapconn->output_handler = -1;
|
|
409
|
|
410 /*clear the write buff*/
|
|
411 msn_soap_free_write_buf(soapconn);
|
|
412
|
|
413 /* Write finish!
|
|
414 * callback for write done
|
|
415 */
|
|
416 if(soapconn->written_cb != NULL){
|
|
417 soapconn->written_cb(soapconn, source, 0);
|
|
418 }
|
|
419 }
|
|
420
|
|
421 /*write the buffer to SOAP connection*/
|
|
422 void
|
|
423 msn_soap_write(MsnSoapConn * soapconn, char *write_buf, GaimInputFunction written_cb)
|
|
424 {
|
|
425 soapconn->write_buf = write_buf;
|
|
426 soapconn->written_len = 0;
|
|
427 soapconn->written_cb = written_cb;
|
19785
|
428
|
|
429 /*clear the read buffer first*/
|
19784
|
430 /*start the write*/
|
|
431 soapconn->output_handler = gaim_input_add(soapconn->gsc->fd, GAIM_INPUT_WRITE,
|
|
432 msn_soap_write_cb, soapconn);
|
|
433 msn_soap_write_cb(soapconn, soapconn->gsc->fd, GAIM_INPUT_WRITE);
|
|
434 }
|
|
435
|
19804
|
436 /* New a soap request*/
|
|
437 MsnSoapReq *
|
|
438 msn_soap_request_new(const char *host,const char *post_url,const char *soap_action,
|
|
439 const char *body,
|
|
440 GaimInputFunction read_cb,GaimInputFunction written_cb)
|
|
441 {
|
|
442 MsnSoapReq *request;
|
|
443
|
|
444 request = g_new0(MsnSoapReq, 1);
|
|
445 request->id = 0;
|
|
446
|
|
447 request->login_host = g_strdup(host);
|
|
448 request->login_path = g_strdup(post_url);
|
|
449 request->soap_action = g_strdup(soap_action);
|
|
450 request->body = g_strdup(body);
|
|
451 request->read_cb = read_cb;
|
|
452 request->written_cb = written_cb;
|
|
453
|
|
454 return request;
|
|
455 }
|
|
456
|
|
457 /*free a soap request*/
|
19785
|
458 void
|
19804
|
459 msn_soap_request_free(MsnSoapReq *request)
|
|
460 {
|
|
461 g_return_if_fail(request != NULL);
|
|
462
|
|
463 g_free(request->login_host);
|
|
464 g_free(request->login_path);
|
|
465 g_free(request->soap_action);
|
|
466 g_free(request->body);
|
|
467 request->read_cb = NULL;
|
|
468 request->written_cb = NULL;
|
|
469
|
|
470 g_free(request);
|
|
471 }
|
|
472
|
|
473 void
|
|
474 msn_soap_post_head_request(MsnSoapConn *soapconn)
|
|
475 {
|
|
476 if(!g_queue_is_empty(soapconn->soap_queue)){
|
|
477 MsnSoapReq *request;
|
|
478
|
|
479 if((request = g_queue_pop_head(soapconn->soap_queue)) != NULL){
|
|
480 msn_soap_post_request(soapconn,request);
|
|
481 }
|
|
482 }
|
|
483 }
|
|
484
|
|
485 void
|
|
486 msn_soap_post(MsnSoapConn *soapconn,MsnSoapReq *request)
|
|
487 {
|
|
488 g_queue_push_tail(soapconn->soap_queue, request);
|
|
489 if(!msn_soap_connected(soapconn)){
|
|
490 /*not connected?connect it first*/
|
|
491 msn_soap_connect(soapconn);
|
|
492 return;
|
|
493 }
|
|
494 /*if connected, what we only needed to do is to queue the request,
|
|
495 * when SOAP request in the queue processed done, will do this command.
|
|
496 * we just waiting...
|
|
497 */
|
|
498 }
|
|
499
|
|
500 /*Post the soap request action*/
|
|
501 void
|
|
502 msn_soap_post_request(MsnSoapConn *soapconn,MsnSoapReq *request)
|
19785
|
503 {
|
|
504 char * soap_head = NULL;
|
|
505 char * request_str = NULL;
|
|
506
|
19790
|
507 gaim_debug_info("MaYuan","msn_soap_post()...\n");
|
19785
|
508 soap_head = g_strdup_printf(
|
|
509 "POST %s HTTP/1.1\r\n"
|
|
510 "SOAPAction: %s\r\n"
|
|
511 "Content-Type:text/xml; charset=utf-8\r\n"
|
|
512 "Cookie: MSPAuth=%s\r\n"
|
|
513 "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n"
|
|
514 "Accept: text/*\r\n"
|
|
515 "Host: %s\r\n"
|
|
516 "Content-Length: %d\r\n"
|
|
517 "Connection: Keep-Alive\r\n"
|
|
518 "Cache-Control: no-cache\r\n\r\n",
|
19804
|
519 request->login_path,
|
|
520 request->soap_action,
|
19785
|
521 soapconn->session->passport_info.mspauth,
|
19804
|
522 request->login_host,
|
|
523 strlen(request->body)
|
19785
|
524 );
|
19804
|
525 request_str = g_strdup_printf("%s%s", soap_head,request->body);
|
19785
|
526 g_free(soap_head);
|
|
527
|
|
528 /*free read buffer*/
|
|
529 msn_soap_free_read_buf(soapconn);
|
19788
|
530 gaim_debug_info("MaYuan","send to server{%s}\n",request_str);
|
19804
|
531 msn_soap_write(soapconn,request_str,request->written_cb);
|
19785
|
532 }
|
|
533
|