Browse code

- script download via Register added

Bogdan-Andrei Iancu authored on 12/09/2003 15:38:56
Showing 3 changed files
... ...
@@ -44,7 +44,7 @@
44 44
 #include "../../sr_module.h"
45 45
 #include "../../str.h"
46 46
 #include "../../dprint.h"
47
-#include "../../error.h"
47
+#include "../../data_lump_rpl.h"
48 48
 #include "../../fifo_server.h"
49 49
 #include "../../parser/parse_uri.h"
50 50
 #include "../../parser/parse_from.h"
... ...
@@ -463,7 +463,7 @@ static int cpl_invoke_script(struct sip_msg* msg, char* str1, char* str2)
463 463
 	}
464 464
 
465 465
 	/* get the script for this user */
466
-	if (get_user_script( db_hdl, &user, &script)==-1)
466
+	if (get_user_script( db_hdl, &user, &script, "cpl_bin")==-1)
467 467
 		goto error;
468 468
 
469 469
 	/* has the user a non-empty script? if not, return normaly, allowing ser to
... ...
@@ -522,6 +522,25 @@ error:
522 522
 #define REMOVE_SCRIPT       0xcaca
523 523
 #define STORE_SCRIPT        0xbebe
524 524
 
525
+#define CONTENT_TYPE_HDR      ("Content-Type: application/cpl-xml"CRLF)
526
+#define CONTENT_TYPE_HDR_LEN  (sizeof(CONTENT_TYPE_HDR)-1)
527
+
528
+struct cpl_error {
529
+	int   err_code;
530
+	char *err_msg;
531
+};
532
+
533
+
534
+//static int   err_code = 400;
535
+//static char *err_msg = "Bad request";
536
+
537
+static struct cpl_error bad_req = {400,"Bad request"};
538
+static struct cpl_error intern_err = {500,"Internal server error"};
539
+static struct cpl_error bad_cpl = {400,"Bad CPL script"};
540
+
541
+static struct cpl_error *cpl_err = &bad_req;
542
+
543
+
525 544
 static inline int do_script_action(struct sip_msg *msg, int action)
526 545
 {
527 546
 	str  body;
... ...
@@ -566,12 +585,16 @@ static inline int do_script_action(struct sip_msg *msg, int action)
566 585
 			}
567 586
 			/* now compile the script and place it into database */
568 587
 			/* get the binary coding for the XML file */
569
-			if ( encodeCPL( &body, &bin)!=1)
588
+			if ( encodeCPL( &body, &bin)!=1) {
589
+				cpl_err = &bad_cpl;
570 590
 				goto error_1;
591
+			}
571 592
 
572 593
 			/* write both the XML and binary formats into database */
573
-			if (write_to_db( db_hdl, user.s, &body, &bin)!=1)
594
+			if (write_to_db( db_hdl, user.s, &body, &bin)!=1) {
595
+				cpl_err = &intern_err;
574 596
 				goto error_1;
597
+			}
575 598
 			break;
576 599
 		case REMOVE_SCRIPT:
577 600
 			/* check the len -> it must be 0 */
... ...
@@ -581,9 +604,10 @@ static inline int do_script_action(struct sip_msg *msg, int action)
581 604
 				goto error_1;
582 605
 			}
583 606
 			/* remove the script for the user */
584
-			if (rmv_from_db( db_hdl, user.s)!=1)
607
+			if (rmv_from_db( db_hdl, user.s)!=1) {
608
+				cpl_err = &intern_err;
585 609
 				goto error_1;
586
-
610
+			}
587 611
 			break;
588 612
 	}
589 613
 
... ...
@@ -597,14 +621,62 @@ error:
597 621
 
598 622
 
599 623
 
600
-static int cpl_process_register(struct sip_msg* msg, char* str, char* str2)
624
+static inline int do_script_download(struct sip_msg *msg)
625
+{
626
+	struct lump_rpl *ct_type;
627
+	struct lump_rpl *body;
628
+	str  user;
629
+	str script;
630
+
631
+	/* get the destination user name */
632
+	if (get_dest_user( msg, &user)==-1)
633
+		goto error;
634
+
635
+	/* get the user's xml script from the database */
636
+	if (get_user_script( db_hdl, &user, &script, "cpl_xml")==-1)
637
+		goto error;
638
+
639
+	/* add a lump with content-type hdr */
640
+	ct_type = build_lump_rpl( CONTENT_TYPE_HDR, CONTENT_TYPE_HDR_LEN,
641
+		LUMP_RPL_HDR);
642
+	if (ct_type==0) {
643
+		LOG(L_ERR,"ERROR:cpl-c:do_script_download: cannot build hdr lump\n");
644
+		cpl_err = &intern_err;
645
+		goto error;
646
+	}
647
+	add_lump_rpl(  msg, ct_type);
648
+
649
+	if (script.len!=0 && script.s!=0) {
650
+		/* user has a script -> add a body lump */
651
+		body = build_lump_rpl( script.s, script.len, LUMP_RPL_BODY);
652
+		if (body==0) {
653
+			LOG(L_ERR,"ERROR:cpl-c:do_script_download: cannot build "
654
+				"body lump\n");
655
+			cpl_err = &intern_err;
656
+			goto error;
657
+		}
658
+		if (add_lump_rpl( msg, body)==-1) {
659
+			LOG(L_CRIT,"BUG:cpl-c:do_script_download: body lump "
660
+				"already added\n");
661
+			cpl_err = &intern_err;
662
+			goto error;
663
+		}
664
+	}
665
+
666
+	return 0;
667
+error:
668
+	return -1;
669
+}
670
+
671
+
672
+
673
+static int cpl_process_register(struct sip_msg* msg, char* str1, char* str2)
601 674
 {
602 675
 	struct disposition *disp;
603 676
 	struct disposition_param *param;
604 677
 	int  ret;
605 678
 	int  mime;
606 679
 	int  *mimes;
607
-	str  user;
608 680
 
609 681
 	/* make sure that is a REGISTER ??? */
610 682
 
... ...
@@ -674,8 +746,7 @@ static int cpl_process_register(struct sip_msg* msg, char* str, char* str2)
674 746
 	}
675 747
 
676 748
 	/* is there an ACCEPT hdr ? */
677
-	ret = parse_accept_hdr( msg );
678
-	if (ret==-1)
749
+	if ( (ret=parse_accept_hdr(msg))==-1)
679 750
 		goto error;
680 751
 	if (ret==0 || (mimes=get_accept(msg))==0 )
681 752
 		/* accept header not present or no mimes found */
... ...
@@ -694,12 +765,11 @@ static int cpl_process_register(struct sip_msg* msg, char* str, char* str2)
694 765
 		/* no accept mime that mached cpl */
695 766
 		goto resume_script;
696 767
 
697
-	/* get the destination user name */
698
-	if (get_dest_user( msg, &user)==-1)
768
+	/* get the user name from msg, retrive the script from db
769
+	 * and appended to reply */
770
+	if (do_script_download( msg )==-1)
699 771
 		goto error;
700 772
 
701
-	/* get the user's script from the database */
702
-
703 773
 	/* send a 200 OK reply back */
704 774
 	sl_reply( msg, (char*)200, "OK");
705 775
 
... ...
@@ -708,6 +778,9 @@ stop_script:
708 778
 resume_script:
709 779
 	return 1;
710 780
 error:
711
-	return -1;
781
+	/* send a error reply back */
782
+	sl_reply( msg, (char*)cpl_err->err_code, cpl_err->err_msg);
783
+	/* I don't want to resturn to script execution, so I return 0 to do break */
784
+	return 0;
712 785
 }
713 786
 
... ...
@@ -38,10 +38,10 @@
38 38
  * Returns:  1 - success
39 39
  *          -1 - error
40 40
  */
41
-int get_user_script( db_con_t *db_hdl, str *user, str *script)
41
+int get_user_script( db_con_t *db_hdl, str *user, str *script, char* key)
42 42
 {
43 43
 	db_key_t   keys_cmp[] = {"user"};
44
-	db_key_t   keys_ret[] = {"cpl_bin"};
44
+	db_key_t   keys_ret[] = { key };
45 45
 	db_val_t   vals[1];
46 46
 	db_res_t   *res = 0 ;
47 47
 	char       tmp;
... ...
@@ -43,7 +43,7 @@ int write_to_db(db_con_t *db_con, char *usr, str *xml, str *bin);
43 43
  * Returns:  1 - success
44 44
  *          -1 - error
45 45
  */
46
-int get_user_script( db_con_t *db_hdl, str *user, str *script);
46
+int get_user_script( db_con_t *db_hdl, str *user, str *script, char *key);
47 47
 
48 48
 
49 49
 /* delete from database the entiry record for a given user - if a user has no