request.rb
2.08 KB
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
# Copyright (C) 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "rexml/document"
module GData
module HTTP
# Very simple class to hold everything about an HTTP request.
class Request
# The URL of the request.
attr_accessor :url
# The body of the request.
attr_accessor :body
# The HTTP method being used in the request.
attr_accessor :method
# The HTTP headers of the request.
attr_accessor :headers
# Only the URL itself is required, everything else is optional.
def initialize(url, options = {})
@url = url
options.each do |key, value|
self.send("#{key}=", value)
end
@method ||= :get
@headers ||= {}
end
# Returns whether or not a request is chunked.
def chunked?
if @headers['Transfer-Encoding'] == 'chunked'
return true
else
return false
end
end
# Sets if the request is using chunked transfer-encoding.
def chunked=(enabled)
if enabled
@headers['Transfer-Encoding'] = 'chunked'
else
@headers.delete('Transfer-Encoding')
end
end
# Calculates and sets the length of the body.
def calculate_length!
if not @headers['Content-Length'] and not chunked? \
and method != :get and method != :delete
if @body
@headers['Content-Length'] = @body.length
else
@headers['Content-Length'] = 0
end
end
end
end
end
end