Conditions | 25 |
Total Lines | 93 |
Code Lines | 70 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like medusa.default_handler.default_handler.handle_request() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # -*- Mode: Python; tab-width: 4 -*- |
||
78 | def handle_request (self, request): |
||
79 | |||
80 | if request.command not in self.valid_commands: |
||
81 | request.error (400) # bad request |
||
82 | return |
||
83 | |||
84 | self.hit_counter.increment() |
||
85 | |||
86 | path, params, query, fragment = request.split_uri() |
||
87 | |||
88 | if '%' in path: |
||
89 | path = unquote (path) |
||
90 | |||
91 | # strip off all leading slashes |
||
92 | while path and path[0] == '/': |
||
93 | path = path[1:] |
||
94 | |||
95 | if self.filesystem.isdir (path): |
||
96 | if path and path[-1] != '/': |
||
97 | request['Location'] = 'http://%s/%s/' % ( |
||
98 | request.channel.server.server_name, |
||
99 | path |
||
100 | ) |
||
101 | request.error (301) |
||
102 | return |
||
103 | |||
104 | # we could also generate a directory listing here, |
||
105 | # may want to move this into another method for that |
||
106 | # purpose |
||
107 | found = 0 |
||
108 | if path and path[-1] != '/': |
||
109 | path = path + '/' |
||
110 | for default in self.directory_defaults: |
||
111 | p = path + default |
||
112 | if self.filesystem.isfile (p): |
||
113 | path = p |
||
114 | found = 1 |
||
115 | break |
||
116 | if not found: |
||
117 | request.error (404) # Not Found |
||
118 | return |
||
119 | |||
120 | elif not self.filesystem.isfile (path): |
||
121 | request.error (404) # Not Found |
||
122 | return |
||
123 | |||
124 | file_length = self.filesystem.stat (path)[stat.ST_SIZE] |
||
125 | |||
126 | ims = get_header_match (IF_MODIFIED_SINCE, request.header) |
||
127 | |||
128 | length_match = 1 |
||
129 | if ims: |
||
130 | length = ims.group (4) |
||
131 | if length: |
||
132 | try: |
||
133 | length = string.atoi (length) |
||
134 | if length != file_length: |
||
135 | length_match = 0 |
||
136 | except: |
||
137 | pass |
||
138 | |||
139 | ims_date = 0 |
||
140 | |||
141 | if ims: |
||
142 | ims_date = http_date.parse_http_date (ims.group (1)) |
||
143 | |||
144 | try: |
||
145 | mtime = self.filesystem.stat (path)[stat.ST_MTIME] |
||
146 | except: |
||
147 | request.error (404) |
||
148 | return |
||
149 | |||
150 | if length_match and ims_date: |
||
151 | if mtime <= ims_date: |
||
152 | request.reply_code = 304 |
||
153 | request.done() |
||
154 | self.cache_counter.increment() |
||
155 | return |
||
156 | try: |
||
157 | file = self.filesystem.open (path, 'rb') |
||
158 | except IOError: |
||
159 | request.error (404) |
||
160 | return |
||
161 | |||
162 | request['Last-Modified'] = http_date.build_http_date (mtime) |
||
163 | request['Content-Length'] = file_length |
||
164 | self.set_content_type (path, request) |
||
165 | |||
166 | if request.command == 'get': |
||
167 | request.push (self.default_file_producer (file)) |
||
168 | |||
169 | self.file_counter.increment() |
||
170 | request.done() |
||
171 | |||
216 |