Class: OdpsDatahub::HttpConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/http/http_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(odpsConfig, headers, params, path, method, stream = nil, isodpsurl = false) ⇒ HttpConnection

Returns a new instance of HttpConnection.



31
32
33
34
35
36
37
38
39
40
# File 'lib/fluent/plugin/http/http_connection.rb', line 31

def initialize(odpsConfig, headers, params, path, method, stream = nil, isodpsurl = false)
  @mOdpsConfig = odpsConfig
  @mHeader = headers
  @mParam = params
  @mPath = path
  @mMethod = method
  @mStream = stream
  @mIsOdpsUrl = isodpsurl
  buildRequest
end

Instance Attribute Details

#mHeaderObject (readonly)

Returns the value of attribute mHeader.



30
31
32
# File 'lib/fluent/plugin/http/http_connection.rb', line 30

def mHeader
  @mHeader
end

#mMethodObject (readonly)

Returns the value of attribute mMethod.



30
31
32
# File 'lib/fluent/plugin/http/http_connection.rb', line 30

def mMethod
  @mMethod
end

#mOdpsConfigObject (readonly)

Returns the value of attribute mOdpsConfig.



30
31
32
# File 'lib/fluent/plugin/http/http_connection.rb', line 30

def mOdpsConfig
  @mOdpsConfig
end

#mParamObject (readonly)

Returns the value of attribute mParam.



30
31
32
# File 'lib/fluent/plugin/http/http_connection.rb', line 30

def mParam
  @mParam
end

#mPathObject (readonly)

Returns the value of attribute mPath.



30
31
32
# File 'lib/fluent/plugin/http/http_connection.rb', line 30

def mPath
  @mPath
end

#mReqObject (readonly)

Returns the value of attribute mReq.



30
31
32
# File 'lib/fluent/plugin/http/http_connection.rb', line 30

def mReq
  @mReq
end

#mStreamObject (readonly)

Returns the value of attribute mStream.



30
31
32
# File 'lib/fluent/plugin/http/http_connection.rb', line 30

def mStream
  @mStream
end

#mUriObject (readonly)

Returns the value of attribute mUri.



30
31
32
# File 'lib/fluent/plugin/http/http_connection.rb', line 30

def mUri
  @mUri
end

Instance Method Details

#buildRequestObject



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
# File 'lib/fluent/plugin/http/http_connection.rb', line 42

def buildRequest
  path = ""
  separater = '?'
  @mParam.each { |key , value|
    if value != ""
      path += separater + key.to_s + '=' + value.to_s
    else
      path += separater + key.to_s
    end
    separater = '&'
  }
  if @mIsOdpsUrl
    @mUri = URI.parse(@mOdpsConfig.odpsEndpoint + @mPath + path)
  else
    @mUri = URI.parse(@mOdpsConfig.datahubEndpoint + @mPath + path)
  end

  if !@mHeader.has_key?($CONTENT_MD5)
    @mHeader[$CONTENT_MD5] = ""
  end
  if !@mHeader.has_key?($CONTENT_TYPE)
    @mHeader[$CONTENT_TYPE] = ""
  end
  @mHeader[$DATE] = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S GMT")
  if not @mIsOdpsUrl
    @mHeader[$TUNNEL_STREAM_VERSION] = "1"
    @mHeader[$TUNNEL_VERSION] = "4"
  end
  @mHeader[$USER_AGENT] = $SDK_UA_STR + @mOdpsConfig.userAgent
  @mHeader[$AUTHORIZATION] = signAuthorization
  case @mMethod
    when "POST"
      @mReq = ::Net::HTTP::Post.new(mUri.to_s, @mHeader)
      @mReq.body = @mStream
    when "GET"
      @mReq = ::Net::HTTP::Get.new(mUri.to_s, @mHeader)
    when "PUT"
      @mReq = ::Net::HTTP::Put.new(mUri.to_s, @mHeader)
      @mReq.body = @mStream
    else
      raise OdpsDatahubException.new($INVALID_ARGUMENT, "invalid method")
  end
end

#getResponseObject



86
87
88
89
90
91
# File 'lib/fluent/plugin/http/http_connection.rb', line 86

def getResponse()
  res = Net::HTTP.start(@mUri.host, @mUri.port) {|http|
    http.request(@mReq)
  }
  return res
end

#signAuthorizationObject



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
# File 'lib/fluent/plugin/http/http_connection.rb', line 93

def signAuthorization
  prefix = "x-odps-"
  stringToSign = @mMethod + "\n"
  accessKey = @mOdpsConfig.accessKey
  headerMapDown = Hash.new

  @mHeader.each { |key , value|
    keyDown = key.downcase
    headerMapDown[keyDown] = value
  }
  headerArray = headerMapDown.sort

  headerArray.each { |key , value|
    if key.start_with?(prefix)
      stringToSign << key << ":" << value
      stringToSign << "\n"
    elsif key == 'content-type' or key == 'content-md5' or key == 'date'
      stringToSign << value
      stringToSign << "\n"
    end
  }

  signParam = ""
  separater = '?'
  paramArray = @mParam.sort
  paramArray.each { |key , value|
    if value != ""
      signParam += separater + key.to_s + '=' + value.to_s
    else
      signParam += separater + key.to_s
    end
    separater = '&'
  }
  stringToSign += @mPath + signParam
  #puts stringToSign
  signedStr =  "ODPS " + @mOdpsConfig.accessId + ":" + Base64.encode64("#{OpenSSL::HMAC.digest('sha1', accessKey, stringToSign)}").to_s
end