Artifact [4af78cf4e6]

Artifact 4af78cf4e694fa94e65df34647720f33e770069c:


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
   100
   101
   102
   103
   104
   105
   106
   107
   108
   109
   110
   111
   112
   113
   114
   115
   116
   117
   118
   119
   120
   121
   122
   123
   124
   125
   126
   127
   128
   129
   130
   131
   132
   133
   134
   135
   136
   137
   138
   139
   140
   141
   142
   143
   144
   145
   146
   147
   148
   149
   150
   151
   152
   153
   154
   155
   156
   157
   158
   159
   160
   161
   162
   163
   164
   165
   166
   167
   168
   169
   170
   171
   172
   173
   174
   175
   176
   177
   178
   179
   180
   181
   182
   183
   184
   185
   186
   187
   188
   189
   190
   191
   192
   193
   194
   195
   196
   197
   198
   199
   200
   201
   202
   203
   204
   205
   206
   207
   208
   209
   210
   211
   212
   213
   214
   215
   216
   217
   218
   219
   220
   221
   222
   223
   224
   225
   226
   227
   228
   229
   230
   231
   232
   233
#! /usr/bin/env crystal

require "openssl"
require "openssl/digest"
require "http/client"
require "file.cr"

#
# Crystal 0.10 has a bug in the OpenSSL::Digest.update method, rewrite it here.
#
module OpenSSL
  module DigestBase
    def update(io : IO)
      buffer :: UInt8[2048]
      while (read_bytes = io.read(buffer.to_slice)) > 0
        self << buffer.to_slice[0, read_bytes]
      end
      self
    end
  end
end

private def getHashInformation(hashMethod : String) : Hash
	begin
		hashData = OpenSSL::Digest.new(hashMethod)
	rescue
		raise "Unacceptable hashing algorithm: #{hashMethod}"
		
	end

	returnValue = {
		length: hashData.digest_size
	}

	return returnValue
end

private def validateFile(fileName, hashMethod, hashValue) : Bool
	checkHash = OpenSSL::Digest.new(hashMethod)

	checkHash.file(fileName)

	if checkHash.hexdigest === hashValue
		return true
	end

	return false
end

private def fetchFile(url : String, cacheFileName, hashMethod, hashValue)
	if (url =~ /^https?:\/\//).nil?
		raise "Invalid URL scheme\nWe only support HTTP, and HTTPS transports for origin URLs.  The URL must begin with one of: http://, https://"
	end

	tempFileName = "#{cacheFileName}-#{Random.rand}-#{Random.rand}-#{Random.rand}"

	Dir.mkdir_p(File.dirname(cacheFileName), mode = 0o755);

	HTTP::Client.get(url) { |result|
		if result.status_code != 200
			raise "Unable to fetch remote file, HTTP status code: #{result.status_code}"
		end

		if ! result.body_io?
			raise "Unable to fetch remote file, no body"
		end

		begin
			tempFile = File.open(tempFileName, "w");

			IO.copy(result.body_io, tempFile.to_fd_io);
		ensure
			if tempFile
				tempFile.close
			end
		end
	}

	if ! validateFile(tempFileName, hashMethod, hashValue)
		if File.exists?(tempFileName)
			File.delete(tempFileName)
		end

		raise "Unable to fetch remote file, validation failed"
	end

	File.rename(tempFileName, cacheFileName)
end

private def deliverFile(fileName)
	file = File.open(fileName)

	IO.copy(file, STDOUT)

	file.close
end

private def handleRequest(hashMethod, hashValue, upstreamURL = Nil)
	hashInfo = getHashInformation(hashMethod)

	if (hashValue.size != (hashInfo[:length] * 2))
		raise "Hash length is incorrect, it should be #{hashInfo[:length] * 2} hex digits long"
	end

	if (hashValue =~ /^[0-9a-f]*$/).nil?
		raise "Hash value is incorrect, it should contain only hex digits"
	end

	cacheFileName = hashMethod + "/" + hashValue

	if File.exists?(cacheFileName)
		return cacheFileName
	end

	if upstreamURL.is_a?(Nil)
		raise "Contents are not locally cached and no upstream URL has been provided"
	end

	fetchFile(upstreamURL, cacheFileName, hashMethod, hashValue)

	return cacheFileName
end

private def parseURL(url : String)
	urlParts = url.split("/")

	case urlParts.size
		when 1
			raise "No hash method or hash value specified."
		when 2
			raise "No hash value specified"
		when 3
			hashMethod = urlParts[1];
			hashValue = urlParts[2];
		else
			raise "Too many components specified in pathname"
	end

	return [hashMethod, hashValue]
end

private def createURL : String
	host = Nil

	["HTTP_HOST", "SERVER_NAME"].each { |hostEntry|
		if ENV[hostEntry]?
			host = ENV[hostEntry]

			break
		end
	}

	if host.is_a?(Nil)
		return "<unknown>"
	end

	if ENV["HTTPS"]? && ENV["HTTPS"] === "on"
		scheme = "https"
	else
		scheme = "http"
	end

	addPort = ""

	if ENV["SERVER_PORT"]?
		defaultPort = if scheme == "https"
			443
		else
			80
		end

		if  ENV["SERVER_PORT"].to_i != defaultPort
			addPort = ":#{ENV["SERVER_PORT"]}"
		end
	end

	return "#{scheme}://#{host}#{addPort}/"
end

private def cgiInterface
	if ENV["HTTP_X_CACHE_URL"]?
		upstreamURL = ENV["HTTP_X_CACHE_URL"]
	end

	begin
		hashMethod, hashValue = parseURL(ENV["REQUEST_URI"])

		outputFile = handleRequest(hashMethod, hashValue, upstreamURL)
	rescue reason
		puts "Status: 400 Not OK"
		puts "Content-type: text/plain"
		puts ""
		puts "Usage: #{createURL()}<hashMethod>/<hashValue>"
		puts "       Supply X-Cache-URL header with the origin URL to cache, it will be fetched and cached if the contents are not already available"
		puts ""
		puts "Example: curl --fail --header 'X-Cache-URL: http://www.rkeene.org/' http://hashcache.rkeene.org/sha1/dfc00e1a9ad78225527028db113d72c0ec8c12f8"
		puts ""
		puts "Error:"
		reason.to_s.split("\n").each { |line|
			puts "\t#{line}"
		}

		return
	end

	if outputFile.is_a?(String)
		if File.exists?(outputFile)
			puts "Status: 200 OK"
			puts "Content-type: application/octet-stream"
			puts ""
			deliverFile(outputFile)

			return
		end
	end

	puts "Status: 400 Not OK"
	puts "Content-type: text/plain"
	puts ""
	puts "Something went terribly wrong"
end

private def httpInterface
	puts "No HTTP interface yet"

	exit 1
end

if ENV["GATEWAY_INTERFACE"]?
	cgiInterface
else
	httpInterface
end