Mercurial > pidgin
comparison libgaim/protocols/msn/soap.c @ 20389:e354528c4163
propagate from branch 'im.pidgin.gaim' (head 70ac931e4936c7916eec18a07fe46a0af0fd7403)
to branch 'im.pidgin.rlaager.merging.soc-msnp13-to-svn18164' (head 5b5cde92182d2a922a8e7e6c2308342a5490a8c9)
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Sun, 15 Apr 2007 02:10:37 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
19842:21cb7a79ac7f | 20389:e354528c4163 |
---|---|
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 | |
29 /*define this Macro to debug soap server action*/ | |
30 #undef MSN_SOAP_DEBUG | |
31 | |
32 /*local function prototype*/ | |
33 void msn_soap_set_process_step(MsnSoapConn *soapconn, MsnSoapStep step); | |
34 | |
35 /*setup the soap process step*/ | |
36 void | |
37 msn_soap_set_process_step(MsnSoapConn *soapconn, MsnSoapStep step) | |
38 { | |
39 soapconn->step = step; | |
40 } | |
41 | |
42 //msn_soap_new(MsnSession *session,gpointer data,int sslconn) | |
43 /*new a soap connection*/ | |
44 MsnSoapConn * | |
45 msn_soap_new(MsnSession *session,gpointer data,int sslconn) | |
46 { | |
47 MsnSoapConn *soapconn; | |
48 | |
49 soapconn = g_new0(MsnSoapConn, 1); | |
50 soapconn->session = session; | |
51 soapconn->parent = data; | |
52 soapconn->ssl_conn = sslconn; | |
53 | |
54 soapconn->gsc = NULL; | |
55 soapconn->input_handler = -1; | |
56 soapconn->output_handler = -1; | |
57 | |
58 msn_soap_set_process_step(soapconn,MSN_SOAP_UNCONNECTED); | |
59 soapconn->soap_queue = g_queue_new(); | |
60 return soapconn; | |
61 } | |
62 | |
63 /*ssl soap connect callback*/ | |
64 void | |
65 msn_soap_connect_cb(gpointer data, GaimSslConnection *gsc, | |
66 GaimInputCondition cond) | |
67 { | |
68 MsnSoapConn * soapconn; | |
69 MsnSession *session; | |
70 | |
71 gaim_debug_info("MaYuan","Soap connection connected!\n"); | |
72 soapconn = data; | |
73 g_return_if_fail(soapconn != NULL); | |
74 | |
75 session = soapconn->session; | |
76 g_return_if_fail(session != NULL); | |
77 | |
78 soapconn->gsc = gsc; | |
79 | |
80 /*connection callback*/ | |
81 if(soapconn->connect_cb != NULL){ | |
82 soapconn->connect_cb(data,gsc,cond); | |
83 } | |
84 | |
85 msn_soap_set_process_step(soapconn,MSN_SOAP_CONNECTED); | |
86 /*we do the SOAP request here*/ | |
87 msn_soap_post_head_request(soapconn); | |
88 } | |
89 | |
90 /*ssl soap error callback*/ | |
91 static void | |
92 msn_soap_error_cb(GaimSslConnection *gsc, GaimSslErrorType error, void *data) | |
93 { | |
94 MsnSoapConn * soapconn = data; | |
95 | |
96 g_return_if_fail(data != NULL); | |
97 gaim_debug_info("MaYuan","Soap connection error!\n"); | |
98 msn_soap_set_process_step(soapconn, MSN_SOAP_UNCONNECTED); | |
99 | |
100 /*error callback*/ | |
101 if(soapconn->error_cb != NULL){ | |
102 soapconn->error_cb(gsc,error,data); | |
103 } | |
104 } | |
105 | |
106 /*init the soap connection*/ | |
107 void | |
108 msn_soap_init(MsnSoapConn *soapconn,char * host,int ssl, | |
109 GaimSslInputFunction connect_cb, | |
110 GaimSslErrorFunction error_cb) | |
111 { | |
112 gaim_debug_info("MaYuan","msn_soap_init...\n"); | |
113 soapconn->login_host = g_strdup(host); | |
114 soapconn->ssl_conn = ssl; | |
115 soapconn->connect_cb = connect_cb; | |
116 soapconn->error_cb = error_cb; | |
117 } | |
118 | |
119 /*connect the soap connection*/ | |
120 void | |
121 msn_soap_connect(MsnSoapConn *soapconn) | |
122 { | |
123 if(soapconn->ssl_conn){ | |
124 gaim_ssl_connect(soapconn->session->account, soapconn->login_host, | |
125 GAIM_SSL_DEFAULT_PORT, msn_soap_connect_cb, msn_soap_error_cb, | |
126 soapconn); | |
127 }else{ | |
128 } | |
129 msn_soap_set_process_step(soapconn,MSN_SOAP_CONNECTING); | |
130 } | |
131 | |
132 /*close the soap connection*/ | |
133 void | |
134 msn_soap_close(MsnSoapConn *soapconn) | |
135 { | |
136 if(soapconn->ssl_conn){ | |
137 if(soapconn->gsc != NULL){ | |
138 gaim_ssl_close(soapconn->gsc); | |
139 soapconn->gsc = NULL; | |
140 } | |
141 }else{ | |
142 } | |
143 msn_soap_set_process_step(soapconn,MSN_SOAP_UNCONNECTED); | |
144 } | |
145 | |
146 /*clean the unhandled SOAP request*/ | |
147 void | |
148 msn_soap_clean_unhandled_request(MsnSoapConn *soapconn) | |
149 { | |
150 MsnSoapReq *request; | |
151 | |
152 g_return_if_fail(soapconn != NULL); | |
153 | |
154 while ((request = g_queue_pop_head(soapconn->soap_queue)) != NULL){ | |
155 msn_soap_request_free(request); | |
156 } | |
157 } | |
158 | |
159 /*destroy the soap connection*/ | |
160 void | |
161 msn_soap_destroy(MsnSoapConn *soapconn) | |
162 { | |
163 if(soapconn->login_host) | |
164 g_free(soapconn->login_host); | |
165 | |
166 if(soapconn->login_path) | |
167 g_free(soapconn->login_path); | |
168 | |
169 /*remove the write handler*/ | |
170 if (soapconn->output_handler > 0){ | |
171 gaim_input_remove(soapconn->output_handler); | |
172 } | |
173 /*remove the read handler*/ | |
174 if (soapconn->input_handler > 0){ | |
175 gaim_input_remove(soapconn->input_handler); | |
176 } | |
177 msn_soap_free_read_buf(soapconn); | |
178 msn_soap_free_write_buf(soapconn); | |
179 | |
180 /*close ssl connection*/ | |
181 msn_soap_close(soapconn); | |
182 | |
183 /*process the unhandled soap request*/ | |
184 msn_soap_clean_unhandled_request(soapconn); | |
185 | |
186 g_queue_free(soapconn->soap_queue); | |
187 g_free(soapconn); | |
188 } | |
189 | |
190 /*check the soap is connected? | |
191 * if connected return 1 | |
192 */ | |
193 int | |
194 msn_soap_connected(MsnSoapConn *soapconn) | |
195 { | |
196 if(soapconn->ssl_conn){ | |
197 return (soapconn->gsc == NULL? 0 : 1); | |
198 } | |
199 return(soapconn->fd>0? 1 : 0); | |
200 } | |
201 | |
202 /*read and append the content to the buffer*/ | |
203 static gssize | |
204 msn_soap_read(MsnSoapConn *soapconn) | |
205 { | |
206 gssize len,requested_len; | |
207 char temp_buf[MSN_SOAP_READ_BUFF_SIZE]; | |
208 | |
209 // requested_len = (soapconn->need_to_read > 0) ? soapconn->need_to_read : MSN_SOAP_READ_BUFF_SIZE; | |
210 requested_len = MSN_SOAP_READ_BUFF_SIZE; | |
211 if(soapconn->ssl_conn){ | |
212 len = gaim_ssl_read(soapconn->gsc, temp_buf,requested_len); | |
213 }else{ | |
214 len = read(soapconn->fd, temp_buf,requested_len); | |
215 } | |
216 if(len >0){ | |
217 soapconn->read_buf = g_realloc(soapconn->read_buf, | |
218 soapconn->read_len + len + 1); | |
219 memcpy(soapconn->read_buf + soapconn->read_len, temp_buf, len); | |
220 soapconn->read_len += len; | |
221 soapconn->read_buf[soapconn->read_len] = '\0'; | |
222 } | |
223 #ifdef MSN_SOAP_DEBUG | |
224 gaim_debug_info("MaYuan","++soap ssl read:{%d}\n",len); | |
225 gaim_debug_info("MaYuan","nexus ssl read:{%s}\n",soapconn->read_buf); | |
226 #endif | |
227 return len; | |
228 } | |
229 | |
230 /*read the whole SOAP server response*/ | |
231 void | |
232 msn_soap_read_cb(gpointer data, gint source, GaimInputCondition cond) | |
233 { | |
234 MsnSoapConn *soapconn = data; | |
235 MsnSession *session; | |
236 int len; | |
237 char * body_start,*body_len; | |
238 char *length_start,*length_end; | |
239 | |
240 // gaim_debug_misc("MaYuan", "soap read cb\n"); | |
241 session = soapconn->session; | |
242 g_return_if_fail(session != NULL); | |
243 | |
244 if (soapconn->input_handler == -1){ | |
245 soapconn->input_handler = gaim_input_add(soapconn->gsc->fd, | |
246 GAIM_INPUT_READ, msn_soap_read_cb, soapconn); | |
247 } | |
248 | |
249 /*read the request header*/ | |
250 len = msn_soap_read(soapconn); | |
251 if (len < 0 && errno == EAGAIN){ | |
252 return; | |
253 }else if (len < 0) { | |
254 gaim_debug_error("msn", "read Error!len:%d\n",len); | |
255 gaim_input_remove(soapconn->input_handler); | |
256 soapconn->input_handler = -1; | |
257 g_free(soapconn->read_buf); | |
258 soapconn->read_buf = NULL; | |
259 soapconn->read_len = 0; | |
260 /* TODO: error handling */ | |
261 return; | |
262 } | |
263 | |
264 if(soapconn->read_buf == NULL){ | |
265 return; | |
266 } | |
267 | |
268 if (strstr(soapconn->read_buf, "HTTP/1.1 302") != NULL) | |
269 { | |
270 /* Redirect. */ | |
271 char *location, *c; | |
272 | |
273 gaim_debug_error("MaYuan", "soap redirect\n"); | |
274 location = strstr(soapconn->read_buf, "Location: "); | |
275 if (location == NULL) | |
276 { | |
277 msn_soap_free_read_buf(soapconn); | |
278 | |
279 return; | |
280 } | |
281 location = strchr(location, ' ') + 1; | |
282 | |
283 if ((c = strchr(location, '\r')) != NULL) | |
284 *c = '\0'; | |
285 | |
286 /* Skip the http:// */ | |
287 if ((c = strchr(location, '/')) != NULL) | |
288 location = c + 2; | |
289 | |
290 if ((c = strchr(location, '/')) != NULL) | |
291 { | |
292 g_free(soapconn->login_path); | |
293 soapconn->login_path = g_strdup(c); | |
294 | |
295 *c = '\0'; | |
296 } | |
297 | |
298 g_free(soapconn->login_host); | |
299 soapconn->login_host = g_strdup(location); | |
300 | |
301 gaim_ssl_connect(session->account, soapconn->login_host, | |
302 GAIM_SSL_DEFAULT_PORT, msn_soap_connect_cb, | |
303 msn_soap_error_cb, soapconn); | |
304 } | |
305 else if (strstr(soapconn->read_buf, "HTTP/1.1 401 Unauthorized") != NULL) | |
306 { | |
307 const char *error; | |
308 | |
309 gaim_debug_error("MaYuan", "soap 401\n"); | |
310 if ((error = strstr(soapconn->read_buf, "WWW-Authenticate")) != NULL) | |
311 { | |
312 if ((error = strstr(error, "cbtxt=")) != NULL) | |
313 { | |
314 const char *c; | |
315 char *temp; | |
316 | |
317 error += strlen("cbtxt="); | |
318 | |
319 if ((c = strchr(error, '\n')) == NULL) | |
320 c = error + strlen(error); | |
321 | |
322 temp = g_strndup(error, c - error); | |
323 error = gaim_url_decode(temp); | |
324 g_free(temp); | |
325 } | |
326 } | |
327 | |
328 msn_session_set_error(session, MSN_ERROR_SERV_UNAVAILABLE, error); | |
329 } | |
330 else if ((strstr(soapconn->read_buf, "HTTP/1.1 200 OK")) | |
331 ||(strstr(soapconn->read_buf, "HTTP/1.1 500"))) | |
332 { | |
333 /*OK! process the SOAP body*/ | |
334 body_start = (char *)g_strstr_len(soapconn->read_buf, soapconn->read_len,"\r\n\r\n"); | |
335 if(!body_start){ | |
336 return; | |
337 } | |
338 body_start += 4; | |
339 | |
340 // gaim_debug_misc("msn", "Soap Read: {%s}\n", soapconn->read_buf); | |
341 | |
342 /* we read the content-length*/ | |
343 length_start = strstr(soapconn->read_buf, "Content-Length: "); | |
344 length_start += strlen("Content-Length: "); | |
345 length_end = strstr(length_start, "\r\n"); | |
346 body_len = g_strndup(length_start,length_end - length_start); | |
347 | |
348 /*setup the conn body */ | |
349 soapconn->body = body_start; | |
350 soapconn->body_len = atoi(body_len); | |
351 #ifdef MSN_SOAP_DEBUG | |
352 gaim_debug_misc("MaYuan","SOAP Read length :%d,body len:%d\n",soapconn->read_len,soapconn->body_len); | |
353 #endif | |
354 soapconn->need_to_read = (body_start - soapconn->read_buf +soapconn->body_len) - soapconn->read_len; | |
355 if(soapconn->need_to_read >0){ | |
356 return; | |
357 } | |
358 g_free(body_len); | |
359 | |
360 /*remove the read handler*/ | |
361 gaim_input_remove(soapconn->input_handler); | |
362 soapconn->input_handler = -1; | |
363 /* | |
364 * close the soap connection,if more soap request came, | |
365 * Just reconnect to do it, | |
366 * | |
367 * To solve the problem described below: | |
368 * When I post the soap request in one socket one after the other, | |
369 * The first read is ok, But the second soap read always got 0 bytes, | |
370 * Weird! | |
371 * */ | |
372 msn_soap_close(soapconn); | |
373 | |
374 /*call the read callback*/ | |
375 if(soapconn->read_cb != NULL){ | |
376 soapconn->read_cb(soapconn,source,0); | |
377 } | |
378 } | |
379 return; | |
380 } | |
381 | |
382 void | |
383 msn_soap_free_read_buf(MsnSoapConn *soapconn) | |
384 { | |
385 if(soapconn->read_buf){ | |
386 g_free(soapconn->read_buf); | |
387 } | |
388 soapconn->read_buf = NULL; | |
389 soapconn->read_len = 0; | |
390 soapconn->need_to_read = 0; | |
391 } | |
392 | |
393 void | |
394 msn_soap_free_write_buf(MsnSoapConn *soapconn) | |
395 { | |
396 if(soapconn->write_buf){ | |
397 g_free(soapconn->write_buf); | |
398 } | |
399 soapconn->write_buf = NULL; | |
400 soapconn->written_len = 0; | |
401 } | |
402 | |
403 /*Soap write process func*/ | |
404 static void | |
405 msn_soap_write_cb(gpointer data, gint source, GaimInputCondition cond) | |
406 { | |
407 MsnSoapConn *soapconn = data; | |
408 int len, total_len; | |
409 | |
410 g_return_if_fail(soapconn != NULL); | |
411 if(soapconn->write_buf == NULL){ | |
412 gaim_debug_error("MaYuan","soap buffer is NULL\n"); | |
413 gaim_input_remove(soapconn->output_handler); | |
414 soapconn->output_handler = -1; | |
415 return; | |
416 } | |
417 total_len = strlen(soapconn->write_buf); | |
418 | |
419 /* | |
420 * write the content to SSL server, | |
421 */ | |
422 len = gaim_ssl_write(soapconn->gsc, | |
423 soapconn->write_buf + soapconn->written_len, | |
424 total_len - soapconn->written_len); | |
425 | |
426 if (len < 0 && errno == EAGAIN) | |
427 return; | |
428 else if (len <= 0){ | |
429 /*SSL write error!*/ | |
430 gaim_input_remove(soapconn->output_handler); | |
431 soapconn->output_handler = -1; | |
432 /* TODO: notify of the error */ | |
433 return; | |
434 } | |
435 soapconn->written_len += len; | |
436 | |
437 if (soapconn->written_len < total_len) | |
438 return; | |
439 | |
440 gaim_input_remove(soapconn->output_handler); | |
441 soapconn->output_handler = -1; | |
442 | |
443 /*clear the write buff*/ | |
444 msn_soap_free_write_buf(soapconn); | |
445 | |
446 /* Write finish! | |
447 * callback for write done | |
448 */ | |
449 if(soapconn->written_cb != NULL){ | |
450 soapconn->written_cb(soapconn, source, 0); | |
451 } | |
452 /*maybe we need to read the input?*/ | |
453 msn_soap_read_cb(soapconn,source,0); | |
454 } | |
455 | |
456 /*write the buffer to SOAP connection*/ | |
457 void | |
458 msn_soap_write(MsnSoapConn * soapconn, char *write_buf, GaimInputFunction written_cb) | |
459 { | |
460 soapconn->write_buf = write_buf; | |
461 soapconn->written_len = 0; | |
462 soapconn->written_cb = written_cb; | |
463 | |
464 /*clear the read buffer first*/ | |
465 /*start the write*/ | |
466 soapconn->output_handler = gaim_input_add(soapconn->gsc->fd, GAIM_INPUT_WRITE, | |
467 msn_soap_write_cb, soapconn); | |
468 msn_soap_write_cb(soapconn, soapconn->gsc->fd, GAIM_INPUT_WRITE); | |
469 } | |
470 | |
471 /* New a soap request*/ | |
472 MsnSoapReq * | |
473 msn_soap_request_new(const char *host,const char *post_url,const char *soap_action, | |
474 const char *body, | |
475 GaimInputFunction read_cb,GaimInputFunction written_cb) | |
476 { | |
477 MsnSoapReq *request; | |
478 | |
479 request = g_new0(MsnSoapReq, 1); | |
480 request->id = 0; | |
481 | |
482 request->login_host = g_strdup(host); | |
483 request->login_path = g_strdup(post_url); | |
484 request->soap_action = g_strdup(soap_action); | |
485 request->body = g_strdup(body); | |
486 request->read_cb = read_cb; | |
487 request->written_cb = written_cb; | |
488 | |
489 return request; | |
490 } | |
491 | |
492 /*free a soap request*/ | |
493 void | |
494 msn_soap_request_free(MsnSoapReq *request) | |
495 { | |
496 g_return_if_fail(request != NULL); | |
497 | |
498 g_free(request->login_host); | |
499 g_free(request->login_path); | |
500 g_free(request->soap_action); | |
501 g_free(request->body); | |
502 request->read_cb = NULL; | |
503 request->written_cb = NULL; | |
504 | |
505 g_free(request); | |
506 } | |
507 | |
508 /*post the soap request queue's head request*/ | |
509 void | |
510 msn_soap_post_head_request(MsnSoapConn *soapconn) | |
511 { | |
512 g_return_if_fail(soapconn->soap_queue != NULL); | |
513 | |
514 if(!g_queue_is_empty(soapconn->soap_queue)){ | |
515 MsnSoapReq *request; | |
516 if((request = g_queue_pop_head(soapconn->soap_queue)) != NULL){ | |
517 msn_soap_post_request(soapconn,request); | |
518 } | |
519 } | |
520 msn_soap_set_process_step(soapconn,MSN_SOAP_CONNECTED_IDLE); | |
521 } | |
522 | |
523 /*post the soap request , | |
524 * if not connected, Connected first. | |
525 */ | |
526 void | |
527 msn_soap_post(MsnSoapConn *soapconn,MsnSoapReq *request, | |
528 MsnSoapConnectInitFunction msn_soap_init_func) | |
529 { | |
530 if(request != NULL){ | |
531 g_queue_push_tail(soapconn->soap_queue, request); | |
532 } | |
533 if(!msn_soap_connected(soapconn)&&(soapconn->step == MSN_SOAP_UNCONNECTED) | |
534 &&(!g_queue_is_empty(soapconn->soap_queue))){ | |
535 /*not connected?and we have something to process connect it first*/ | |
536 gaim_debug_info("Ma Yuan","soap is not connected!\n"); | |
537 msn_soap_init_func(soapconn); | |
538 msn_soap_connect(soapconn); | |
539 return; | |
540 } | |
541 gaim_debug_info("Ma Yuan","soap connected!\n"); | |
542 | |
543 /*if connected, what we only needed to do is to queue the request, | |
544 * when SOAP request in the queue processed done, will do this command. | |
545 * we just waiting... | |
546 * If we send the request this time,error may occure | |
547 */ | |
548 #if 0 | |
549 if(soapconn->step == MSN_SOAP_CONNECTED_IDLE){ | |
550 msn_soap_post_head_request(soapconn); | |
551 } | |
552 #endif | |
553 } | |
554 | |
555 /*Post the soap request action*/ | |
556 void | |
557 msn_soap_post_request(MsnSoapConn *soapconn,MsnSoapReq *request) | |
558 { | |
559 char * soap_head = NULL; | |
560 char * request_str = NULL; | |
561 | |
562 gaim_debug_info("MaYuan","msn_soap_post_request()...\n"); | |
563 msn_soap_set_process_step(soapconn,MSN_SOAP_PROCESSING); | |
564 soap_head = g_strdup_printf( | |
565 "POST %s HTTP/1.1\r\n" | |
566 "SOAPAction: %s\r\n" | |
567 "Content-Type:text/xml; charset=utf-8\r\n" | |
568 "Cookie: MSPAuth=%s\r\n" | |
569 "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n" | |
570 "Accept: */*\r\n" | |
571 "Host: %s\r\n" | |
572 "Content-Length: %d\r\n" | |
573 "Connection: Keep-Alive\r\n" | |
574 "Cache-Control: no-cache\r\n\r\n", | |
575 request->login_path, | |
576 request->soap_action, | |
577 soapconn->session->passport_info.mspauth, | |
578 request->login_host, | |
579 strlen(request->body) | |
580 ); | |
581 request_str = g_strdup_printf("%s%s", soap_head,request->body); | |
582 g_free(soap_head); | |
583 | |
584 #ifdef MSN_SOAP_DEBUG | |
585 gaim_debug_info("MaYuan","send to server{%s}\n",request_str); | |
586 #endif | |
587 | |
588 /*free read buffer*/ | |
589 msn_soap_free_read_buf(soapconn); | |
590 /*post it to server*/ | |
591 msn_soap_write(soapconn,request_str,request->written_cb); | |
592 } | |
593 |