| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | require "open3" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  | module Hbc | 
					
						
							|  |  |  |   class Fetcher | 
					
						
							|  |  |  |     TIMEOUT = 10
 | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def self.head(url) | 
					
						
							| 
									
										
										
										
											2016-10-14 20:03:34 +02:00
										 |  |  |       if url.to_s =~ /googlecode/ | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         googlecode_fake_head(url) | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         SystemCommand.run("/usr/bin/curl", | 
					
						
							|  |  |  |                           args: ["--max-time", TIMEOUT, "--silent", "--location", "--head", url]).stdout | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     # google code does not properly respond to HTTP HEAD requests, like a jerk | 
					
						
							|  |  |  |     # this fakes a HEAD by doing a GET, taking the first 20 lines, then running away | 
					
						
							|  |  |  |     def self.googlecode_fake_head(url) | 
					
						
							|  |  |  |       command = "curl --max-time #{TIMEOUT} --verbose --location '#{url}' | head -n 20 > /dev/null" | 
					
						
							|  |  |  |       stderr = Open3.capture3(command)[1] | 
					
						
							| 
									
										
										
										
											2016-10-14 20:03:34 +02:00
										 |  |  |       stderr.split("\n").grep(/^< /).map { |line| line.sub(/^< /, "") }.join("\n") | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | end |