Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/remote/http/curb.rb
1990 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
require 'curb'
21
22
module Selenium
23
module WebDriver
24
module Remote
25
module Http
26
#
27
# An alternative to the default Net::HTTP client.
28
#
29
# This can be used for the Firefox and Remote drivers if you have Curb
30
# installed.
31
#
32
# @example Using Curb
33
# require 'selenium/webdriver/remote/http/curb'
34
# include Selenium
35
#
36
# driver = WebDriver.for :firefox, :http_client => WebDriver::Remote::Http::Curb.new
37
#
38
39
class Curb < Common
40
attr_accessor :timeout
41
42
def initialize(timeout: nil)
43
@timeout = timeout
44
super()
45
end
46
47
def quit_errors
48
[Curl::Err::RecvError] + super
49
end
50
51
private
52
53
def request(verb, url, headers, payload)
54
client.url = url.to_s
55
56
# workaround for http://github.com/taf2/curb/issues/issue/40
57
# curb will handle this for us anyway
58
headers.delete 'Content-Length'
59
60
client.headers = headers
61
62
# http://github.com/taf2/curb/issues/issue/33
63
client.head = false
64
client.delete = false
65
66
case verb
67
when :get
68
client.http_get
69
when :post
70
client.post_body = payload || ''
71
client.http_post
72
when :put
73
client.put_data = payload || ''
74
client.http_put
75
when :delete
76
client.http_delete
77
when :head
78
client.http_head
79
else
80
raise Error::WebDriverError, "unknown HTTP verb: #{verb.inspect}"
81
end
82
83
create_response client.response_code, client.body_str, client.content_type
84
end
85
86
def client
87
@client ||= begin
88
c = Curl::Easy.new
89
90
c.max_redirects = MAX_REDIRECTS
91
c.follow_location = true
92
c.timeout = timeout if timeout
93
c.verbose = WebDriver.logger.debug?
94
c
95
end
96
end
97
end # Curb
98
end # Http
99
end # Remote
100
end # WebDriver
101
end # Selenium
102
103