comparison lib/protocols.c @ 676:72a6de68d9c8

2005-1-24 Brian Masney <masneyb@gftp.org> * lib/protocols.c (gftp_parse_url) - rewrote the URL parser so that the URL is parsed from right to left instead of left to right. There are more checks done to the URL.
author masneyb
date Tue, 25 Jan 2005 01:55:01 +0000
parents 8ff1c1647b95
children 8990a8a26ccf
comparison
equal deleted inserted replaced
675:84d38c525f46 676:72a6de68d9c8
784 784
785 785
786 int 786 int
787 gftp_parse_url (gftp_request * request, const char *url) 787 gftp_parse_url (gftp_request * request, const char *url)
788 { 788 {
789 char *pos, *endpos, *endhostpos, tempchar, *default_protocol, *stpos; 789 char *pos, *endpos, *default_protocol, *new_url;
790 gftp_logging_func logging_function; 790 gftp_logging_func logging_function;
791 const char *cpos; 791 const char *clear_pos;
792 int i, init_ret; 792 int i, ret;
793 size_t len;
794 793
795 g_return_val_if_fail (request != NULL, GFTP_EFATAL); 794 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
796 g_return_val_if_fail (url != NULL, GFTP_EFATAL); 795 g_return_val_if_fail (url != NULL, GFTP_EFATAL);
797 796
798 logging_function = request->logging_function; 797 logging_function = request->logging_function;
799 gftp_request_destroy (request, 0); 798 gftp_request_destroy (request, 0);
800 request->logging_function = logging_function; 799 request->logging_function = logging_function;
801 800
802 for (cpos = url; *cpos == ' '; cpos++); 801 for (clear_pos = url;
803 stpos = g_strdup (cpos); 802 *clear_pos == ' ' || *clear_pos == '\t';
804 for (pos = stpos + strlen (stpos) - 1; *pos == ' '; pos--) 803 clear_pos++);
804
805 new_url = g_strdup (clear_pos);
806
807 for (pos = new_url + strlen (new_url) - 1;
808 *pos == ' ' || *pos == '\t';
809 pos--)
805 *pos = '\0'; 810 *pos = '\0';
806 811
807 i = GFTP_FTP_NUM; 812 /* See if the URL has a protocol... */
808 813 if ((pos = strstr (new_url, "://")) != NULL)
809 if ((pos = strstr (stpos, "://")) != NULL)
810 { 814 {
811 *pos = '\0'; 815 *pos = '\0';
812 816
813 for (i = 0; gftp_protocols[i].url_prefix; i++) 817 for (i = 0; gftp_protocols[i].url_prefix; i++)
814 { 818 {
815 if (strcmp (gftp_protocols[i].url_prefix, stpos) == 0) 819 if (strcmp (gftp_protocols[i].url_prefix, new_url) == 0)
816 break; 820 break;
817 } 821 }
818 822
819 if (gftp_protocols[i].url_prefix == NULL) 823 if (gftp_protocols[i].url_prefix == NULL)
820 { 824 {
821 request->logging_function (gftp_logging_misc, NULL, 825 request->logging_function (gftp_logging_misc, NULL,
822 _("The protocol '%s' is currently not supported.\n"), 826 _("The protocol '%s' is currently not supported.\n"),
823 stpos); 827 new_url);
824 g_free (stpos); 828 g_free (new_url);
825 return (GFTP_EFATAL); 829 return (GFTP_EFATAL);
826 } 830 }
827 831
828 *pos = ':'; 832 *pos = ':';
833 pos += 3;
829 } 834 }
830 else 835 else
831 { 836 {
832 gftp_lookup_request_option (request, "default_protocol", 837 gftp_lookup_request_option (request, "default_protocol",
833 &default_protocol); 838 &default_protocol);
834 839
840 i = GFTP_FTP_NUM;
835 if (default_protocol != NULL && *default_protocol != '\0') 841 if (default_protocol != NULL && *default_protocol != '\0')
836 { 842 {
837 for (i = 0; gftp_protocols[i].url_prefix; i++) 843 for (i = 0; gftp_protocols[i].url_prefix; i++)
838 { 844 {
839 if (strcmp (gftp_protocols[i].name, default_protocol) == 0) 845 if (strcmp (gftp_protocols[i].name, default_protocol) == 0)
840 break; 846 break;
841 } 847 }
842 } 848 }
843 } 849
844 850 if (gftp_protocols[i].url_prefix == NULL)
845 if (gftp_protocols[i].url_prefix == NULL) 851 {
846 i = GFTP_FTP_NUM; 852 request->logging_function (gftp_logging_misc, NULL,
847 853 _("The protocol '%s' is currently not supported.\n"),
848 if ((init_ret = gftp_protocols[i].init (request)) < 0) 854 default_protocol);
855 g_free (new_url);
856 return (GFTP_EFATAL);
857 }
858
859 pos = new_url;
860 }
861
862 if ((ret = gftp_protocols[i].init (request)) < 0)
849 { 863 {
850 gftp_request_destroy (request, 0); 864 gftp_request_destroy (request, 0);
851 return (init_ret); 865 return (ret);
866 }
867
868 if ((endpos = strchr (pos, '/')) != NULL)
869 {
870 gftp_set_directory (request, endpos);
871 *endpos = '\0';
852 } 872 }
853 873
854 if (request->parse_url != NULL) 874 if (request->parse_url != NULL)
855 { 875 {
856 g_free (stpos); 876 ret = request->parse_url (request, new_url);
857 return (request->parse_url (request, url)); 877 g_free (new_url);
858 } 878 return (ret);
859 879 }
860 pos = stpos; 880
861 len = strlen (request->url_prefix); 881 if (*pos != '\0')
862 if (strncmp (pos, request->url_prefix, len) == 0 && 882 {
863 strncmp (pos + len, "://", 3) == 0) 883 if (endpos == NULL)
864 pos += len + 3; 884 endpos = pos + strlen (pos) - 1;
865 885 else
866 if (i == GFTP_LOCAL_NUM) 886 endpos--;
867 { 887
868 for (; *pos == ' ' || *pos == '\t'; pos++); 888 for (; isdigit (*endpos); endpos--);
869 request->directory = g_strdup (pos); 889
870 g_free (stpos); 890 if (*endpos == ':' && isdigit (*(endpos + 1)))
871 return (0); 891 {
872 } 892 gftp_set_port (request, strtol (endpos + 1, NULL, 10));
873
874 if ((endhostpos = strrchr (pos, '@')) != NULL)
875 {
876 /* A user/password was entered */
877 if ((endpos = strchr (pos, ':')) == NULL || endhostpos < endpos)
878 {
879 /* No password was entered */
880 gftp_set_password (request, "");
881 endpos = endhostpos;
882 }
883
884 *endpos = '\0';
885 gftp_set_username (request, pos);
886
887 pos = endpos + 1;
888 if ((endpos = strchr (pos, '@')) != NULL)
889 {
890 if (strchr (endpos + 1, '@') != NULL)
891 endpos = strchr (endpos + 1, '@');
892 *endpos = '\0'; 893 *endpos = '\0';
893 gftp_set_password (request, pos); 894 }
894 895
895 pos = endpos + 1; 896 if ((endpos = strrchr (pos, '@')) != NULL)
896 } 897 {
897 } 898 gftp_set_hostname (request, endpos + 1);
898 899 *endpos = '\0';
899 /* Now get the hostname and an optional port and optional directory */ 900
900 endhostpos = pos + strlen (pos); 901 if ((endpos = strchr (pos, ':')) != NULL)
901 if ((endpos = strchr (pos, ':')) != NULL) 902 {
902 endhostpos = endpos; 903 *endpos = '\0';
903 else if ((endpos = strchr (pos, '/')) != NULL) 904 gftp_set_username (request, pos);
904 endhostpos = endpos; 905 gftp_set_password (request, endpos + 1);
905 tempchar = *endhostpos; 906 }
906 *endhostpos = '\0'; 907 else
907 gftp_set_hostname (request, pos); 908 {
908 *endhostpos = tempchar; 909 gftp_set_username (request, pos);
909 910 gftp_set_password (request, "");
910 if ((endpos = strchr (pos, ':')) != NULL) 911 }
911 { 912 }
912 /* A port was entered */ 913 else
913 pos = endpos + 1; 914 gftp_set_hostname (request, pos);
914 gftp_set_port (request, strtol (pos, NULL, 10)); 915 }
915 } 916
916 if ((endpos = strchr (pos, '/')) != NULL) 917 g_free (new_url);
917 gftp_set_directory (request, endpos);
918 g_free (stpos);
919 return (0); 918 return (0);
920 } 919 }
921 920
922 921
923 void 922 void