@matheusbsbs / matheus-bagliano / commits / c2394fe934
Fork 0

gitweb: Put all per-connection code in run() subroutine

All code that is run per-connection (as opposed to those parts of gitweb code that can be run once) is put into appropriate subroutines: - evaluate_uri - evaluate_gitweb_config - evaluate_git_version (here only because $GIT can be set in config) - check_loadavg (as soon as possible; $git_version must be defined) - evaluate_query_params (counterpart to evaluate_path_info) - evaluate_and_validate_params - evaluate_git_dir (requires $project) - configure_gitweb_features (@snapshot_fmts, $git_avatar) - dispatch (includes setting default $action) The difference is best viewed with '-w', '--ignore-all-space' option, because of reindent caused by putting code in subroutines. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jakub Narebski committed May 7, 2010 at 14:54 UTC c2394fe93441618056363a14c8b63f526afa9615
1 file changed +204 -153
gitweb/gitweb.perl
+204 -153
@@ -28,34 +28,42 @@ BEGIN {
28 CGI->compile() if $ENV{'MOD_PERL'};
29 }
30
31 -our $cgi = new CGI;
31 our $version = "++GIT_VERSION++";
33 -our $my_url = $cgi->url();
34 -our $my_uri = $cgi->url(-absolute => 1);
32
36 -# Base URL for relative URLs in gitweb ($logo, $favicon, ...),
37 -# needed and used only for URLs with nonempty PATH_INFO
38 -our $base_url = $my_url;
33 +our ($my_url, $my_uri, $base_url, $path_info, $home_link);
34 +sub evaluate_uri {
35 + our $cgi;
36
40 -# When the script is used as DirectoryIndex, the URL does not contain the name
41 -# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
42 -# have to do it ourselves. We make $path_info global because it's also used
43 -# later on.
44 -#
45 -# Another issue with the script being the DirectoryIndex is that the resulting
46 -# $my_url data is not the full script URL: this is good, because we want
47 -# generated links to keep implying the script name if it wasn't explicitly
48 -# indicated in the URL we're handling, but it means that $my_url cannot be used
49 -# as base URL.
50 -# Therefore, if we needed to strip PATH_INFO, then we know that we have
51 -# to build the base URL ourselves:
52 -our $path_info = $ENV{"PATH_INFO"};
53 -if ($path_info) {
54 - if ($my_url =~ s,\Q$path_info\E$,, &&
55 - $my_uri =~ s,\Q$path_info\E$,, &&
56 - defined $ENV{'SCRIPT_NAME'}) {
57 - $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
37 + our $my_url = $cgi->url();
38 + our $my_uri = $cgi->url(-absolute => 1);
39 +
40 + # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
41 + # needed and used only for URLs with nonempty PATH_INFO
42 + our $base_url = $my_url;
43 +
44 + # When the script is used as DirectoryIndex, the URL does not contain the name
45 + # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
46 + # have to do it ourselves. We make $path_info global because it's also used
47 + # later on.
48 + #
49 + # Another issue with the script being the DirectoryIndex is that the resulting
50 + # $my_url data is not the full script URL: this is good, because we want
51 + # generated links to keep implying the script name if it wasn't explicitly
52 + # indicated in the URL we're handling, but it means that $my_url cannot be used
53 + # as base URL.
54 + # Therefore, if we needed to strip PATH_INFO, then we know that we have
55 + # to build the base URL ourselves:
56 + our $path_info = $ENV{"PATH_INFO"};
57 + if ($path_info) {
58 + if ($my_url =~ s,\Q$path_info\E$,, &&
59 + $my_uri =~ s,\Q$path_info\E$,, &&
60 + defined $ENV{'SCRIPT_NAME'}) {
61 + $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
62 + }
63 }
64 +
65 + # target of the home link on top of all pages
66 + our $home_link = $my_uri || "/";
67 }
68
69 # core git executable to use
@@ -70,9 +78,6 @@ our $projectroot = "++GITWEB_PROJECTROOT++";
78 # the number is relative to the projectroot
79 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
80
73 -# target of the home link on top of all pages
74 -our $home_link = $my_uri || "/";
75 -
81 # string of the home link on top of all pages
82 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
83
@@ -553,15 +558,18 @@ sub filter_snapshot_fmts {
558 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
559 }
560
556 -our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
557 -our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
558 -# die if there are errors parsing config file
559 -if (-e $GITWEB_CONFIG) {
560 - do $GITWEB_CONFIG;
561 - die $@ if $@;
562 -} elsif (-e $GITWEB_CONFIG_SYSTEM) {
563 - do $GITWEB_CONFIG_SYSTEM;
564 - die $@ if $@;
561 +our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM);
562 +sub evaluate_gitweb_config {
563 + our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
564 + our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
565 + # die if there are errors parsing config file
566 + if (-e $GITWEB_CONFIG) {
567 + do $GITWEB_CONFIG;
568 + die $@ if $@;
569 + } elsif (-e $GITWEB_CONFIG_SYSTEM) {
570 + do $GITWEB_CONFIG_SYSTEM;
571 + die $@ if $@;
572 + }
573 }
574
575 # Get loadavg of system, to compare against $maxload.
@@ -587,13 +595,16 @@ sub get_loadavg {
595 }
596
597 # version of the core git binary
590 -our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
591 -$number_of_git_cmds++;
592 -
593 -$projects_list ||= $projectroot;
598 +our $git_version;
599 +sub evaluate_git_version {
600 + our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
601 + $number_of_git_cmds++;
602 +}
603
595 -if (defined $maxload && get_loadavg() > $maxload) {
596 - die_error(503, "The load average on the server is too high");
604 +sub check_loadavg {
605 + if (defined $maxload && get_loadavg() > $maxload) {
606 + die_error(503, "The load average on the server is too high");
607 + }
608 }
609
610 # ======================================================================
@@ -680,11 +691,15 @@ our %allowed_options = (
691 # should be single values, but opt can be an array. We should probably
692 # build an array of parameters that can be multi-valued, but since for the time
693 # being it's only this one, we just single it out
683 -while (my ($name, $symbol) = each %cgi_param_mapping) {
684 - if ($symbol eq 'opt') {
685 - $input_params{$name} = [ $cgi->param($symbol) ];
686 - } else {
687 - $input_params{$name} = $cgi->param($symbol);
694 +sub evaluate_query_params {
695 + our $cgi;
696 +
697 + while (my ($name, $symbol) = each %cgi_param_mapping) {
698 + if ($symbol eq 'opt') {
699 + $input_params{$name} = [ $cgi->param($symbol) ];
700 + } else {
701 + $input_params{$name} = $cgi->param($symbol);
702 + }
703 }
704 }
705
@@ -831,149 +846,185 @@ sub evaluate_path_info {
846 }
847 }
848 }
834 -evaluate_path_info();
849
836 -our $action = $input_params{'action'};
837 -if (defined $action) {
838 - if (!validate_action($action)) {
839 - die_error(400, "Invalid action parameter");
850 +our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
851 + $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
852 + $searchtext, $search_regexp);
853 +sub evaluate_and_validate_params {
854 + our $action = $input_params{'action'};
855 + if (defined $action) {
856 + if (!validate_action($action)) {
857 + die_error(400, "Invalid action parameter");
858 + }
859 }
841 -}
860
843 -# parameters which are pathnames
844 -our $project = $input_params{'project'};
845 -if (defined $project) {
846 - if (!validate_project($project)) {
847 - undef $project;
848 - die_error(404, "No such project");
861 + # parameters which are pathnames
862 + our $project = $input_params{'project'};
863 + if (defined $project) {
864 + if (!validate_project($project)) {
865 + undef $project;
866 + die_error(404, "No such project");
867 + }
868 }
850 -}
869
852 -our $file_name = $input_params{'file_name'};
853 -if (defined $file_name) {
854 - if (!validate_pathname($file_name)) {
855 - die_error(400, "Invalid file parameter");
870 + our $file_name = $input_params{'file_name'};
871 + if (defined $file_name) {
872 + if (!validate_pathname($file_name)) {
873 + die_error(400, "Invalid file parameter");
874 + }
875 }
857 -}
876
859 -our $file_parent = $input_params{'file_parent'};
860 -if (defined $file_parent) {
861 - if (!validate_pathname($file_parent)) {
862 - die_error(400, "Invalid file parent parameter");
877 + our $file_parent = $input_params{'file_parent'};
878 + if (defined $file_parent) {
879 + if (!validate_pathname($file_parent)) {
880 + die_error(400, "Invalid file parent parameter");
881 + }
882 }
864 -}
883
866 -# parameters which are refnames
867 -our $hash = $input_params{'hash'};
868 -if (defined $hash) {
869 - if (!validate_refname($hash)) {
870 - die_error(400, "Invalid hash parameter");
884 + # parameters which are refnames
885 + our $hash = $input_params{'hash'};
886 + if (defined $hash) {
887 + if (!validate_refname($hash)) {
888 + die_error(400, "Invalid hash parameter");
889 + }
890 }
872 -}
891
874 -our $hash_parent = $input_params{'hash_parent'};
875 -if (defined $hash_parent) {
876 - if (!validate_refname($hash_parent)) {
877 - die_error(400, "Invalid hash parent parameter");
892 + our $hash_parent = $input_params{'hash_parent'};
893 + if (defined $hash_parent) {
894 + if (!validate_refname($hash_parent)) {
895 + die_error(400, "Invalid hash parent parameter");
896 + }
897 }
879 -}
898
881 -our $hash_base = $input_params{'hash_base'};
882 -if (defined $hash_base) {
883 - if (!validate_refname($hash_base)) {
884 - die_error(400, "Invalid hash base parameter");
899 + our $hash_base = $input_params{'hash_base'};
900 + if (defined $hash_base) {
901 + if (!validate_refname($hash_base)) {
902 + die_error(400, "Invalid hash base parameter");
903 + }
904 }
886 -}
905
888 -our @extra_options = @{$input_params{'extra_options'}};
889 -# @extra_options is always defined, since it can only be (currently) set from
890 -# CGI, and $cgi->param() returns the empty array in array context if the param
891 -# is not set
892 -foreach my $opt (@extra_options) {
893 - if (not exists $allowed_options{$opt}) {
894 - die_error(400, "Invalid option parameter");
895 - }
896 - if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
897 - die_error(400, "Invalid option parameter for this action");
906 + our @extra_options = @{$input_params{'extra_options'}};
907 + # @extra_options is always defined, since it can only be (currently) set from
908 + # CGI, and $cgi->param() returns the empty array in array context if the param
909 + # is not set
910 + foreach my $opt (@extra_options) {
911 + if (not exists $allowed_options{$opt}) {
912 + die_error(400, "Invalid option parameter");
913 + }
914 + if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
915 + die_error(400, "Invalid option parameter for this action");
916 + }
917 }
899 -}
918
901 -our $hash_parent_base = $input_params{'hash_parent_base'};
902 -if (defined $hash_parent_base) {
903 - if (!validate_refname($hash_parent_base)) {
904 - die_error(400, "Invalid hash parent base parameter");
919 + our $hash_parent_base = $input_params{'hash_parent_base'};
920 + if (defined $hash_parent_base) {
921 + if (!validate_refname($hash_parent_base)) {
922 + die_error(400, "Invalid hash parent base parameter");
923 + }
924 }
906 -}
925
908 -# other parameters
909 -our $page = $input_params{'page'};
910 -if (defined $page) {
911 - if ($page =~ m/[^0-9]/) {
912 - die_error(400, "Invalid page parameter");
926 + # other parameters
927 + our $page = $input_params{'page'};
928 + if (defined $page) {
929 + if ($page =~ m/[^0-9]/) {
930 + die_error(400, "Invalid page parameter");
931 + }
932 }
914 -}
933
916 -our $searchtype = $input_params{'searchtype'};
917 -if (defined $searchtype) {
918 - if ($searchtype =~ m/[^a-z]/) {
919 - die_error(400, "Invalid searchtype parameter");
934 + our $searchtype = $input_params{'searchtype'};
935 + if (defined $searchtype) {
936 + if ($searchtype =~ m/[^a-z]/) {
937 + die_error(400, "Invalid searchtype parameter");
938 + }
939 }
921 -}
940
923 -our $search_use_regexp = $input_params{'search_use_regexp'};
941 + our $search_use_regexp = $input_params{'search_use_regexp'};
942
925 -our $searchtext = $input_params{'searchtext'};
926 -our $search_regexp;
927 -if (defined $searchtext) {
928 - if (length($searchtext) < 2) {
929 - die_error(403, "At least two characters are required for search parameter");
943 + our $searchtext = $input_params{'searchtext'};
944 + our $search_regexp;
945 + if (defined $searchtext) {
946 + if (length($searchtext) < 2) {
947 + die_error(403, "At least two characters are required for search parameter");
948 + }
949 + $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
950 }
931 - $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
951 }
952
953 # path to the current git repository
954 our $git_dir;
936 -$git_dir = "$projectroot/$project" if $project;
955 +sub evaluate_git_dir {
956 + our $git_dir = "$projectroot/$project" if $project;
957 +}
958
938 -# list of supported snapshot formats
939 -our @snapshot_fmts = gitweb_get_feature('snapshot');
940 -@snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
959 +our (@snapshot_fmts, $git_avatar);
960 +sub configure_gitweb_features {
961 + # list of supported snapshot formats
962 + our @snapshot_fmts = gitweb_get_feature('snapshot');
963 + @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
964
942 -# check that the avatar feature is set to a known provider name,
943 -# and for each provider check if the dependencies are satisfied.
944 -# if the provider name is invalid or the dependencies are not met,
945 -# reset $git_avatar to the empty string.
946 -our ($git_avatar) = gitweb_get_feature('avatar');
947 -if ($git_avatar eq 'gravatar') {
948 - $git_avatar = '' unless (eval { require Digest::MD5; 1; });
949 -} elsif ($git_avatar eq 'picon') {
950 - # no dependencies
951 -} else {
952 - $git_avatar = '';
965 + # check that the avatar feature is set to a known provider name,
966 + # and for each provider check if the dependencies are satisfied.
967 + # if the provider name is invalid or the dependencies are not met,
968 + # reset $git_avatar to the empty string.
969 + our ($git_avatar) = gitweb_get_feature('avatar');
970 + if ($git_avatar eq 'gravatar') {
971 + $git_avatar = '' unless (eval { require Digest::MD5; 1; });
972 + } elsif ($git_avatar eq 'picon') {
973 + # no dependencies
974 + } else {
975 + $git_avatar = '';
976 + }
977 }
978
979 # dispatch
956 -if (!defined $action) {
957 - if (defined $hash) {
958 - $action = git_get_type($hash);
959 - } elsif (defined $hash_base && defined $file_name) {
960 - $action = git_get_type("$hash_base:$file_name");
961 - } elsif (defined $project) {
962 - $action = 'summary';
963 - } else {
964 - $action = 'project_list';
980 +sub dispatch {
981 + if (!defined $action) {
982 + if (defined $hash) {
983 + $action = git_get_type($hash);
984 + } elsif (defined $hash_base && defined $file_name) {
985 + $action = git_get_type("$hash_base:$file_name");
986 + } elsif (defined $project) {
987 + $action = 'summary';
988 + } else {
989 + $action = 'project_list';
990 + }
991 }
992 + if (!defined($actions{$action})) {
993 + die_error(400, "Unknown action");
994 + }
995 + if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
996 + !$project) {
997 + die_error(400, "Project needed");
998 + }
999 + $actions{$action}->();
1000 }
967 -if (!defined($actions{$action})) {
968 - die_error(400, "Unknown action");
969 -}
970 -if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
971 - !$project) {
972 - die_error(400, "Project needed");
1001 +
1002 +sub run {
1003 + our $t0 = [Time::HiRes::gettimeofday()]
1004 + if defined $t0;
1005 +
1006 + evaluate_uri();
1007 + evaluate_gitweb_config();
1008 + evaluate_git_version();
1009 + check_loadavg();
1010 +
1011 + # $projectroot and $projects_list might be set in gitweb config file
1012 + $projects_list ||= $projectroot;
1013 +
1014 + evaluate_query_params();
1015 + evaluate_path_info();
1016 + evaluate_and_validate_params();
1017 + evaluate_git_dir();
1018 +
1019 + configure_gitweb_features();
1020 +
1021 + dispatch();
1022 +
1023 + DONE_GITWEB:
1024 + 1;
1025 }
974 -$actions{$action}->();
975 -DONE_GITWEB:
976 -1;
1026 +our $cgi = CGI->new();
1027 +run();
1028
1029 ## ======================================================================
1030 ## action links