Class: Net::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/mechanize/monkey_patch.rb,
lib/mechanize/test_case.rb

Overview

:stopdoc:

Constant Summary collapse

SERVLETS =
{
  '/gzip'                   => GzipServlet,
  '/form_post'              => FormServlet,
  '/basic_auth'             => BasicAuthServlet,
  '/form post'              => FormServlet,
  '/response_code'          => ResponseCodeServlet,
  '/http_refresh'           => HttpRefreshServlet,
  '/content_type_test'      => ContentTypeServlet,
  '/referer'                => RefererServlet,
  '/file_upload'            => FileUploadServlet,
  '/one_cookie'             => OneCookieServlet,
  '/one_cookie_no_space'    => OneCookieNoSpacesServlet,
  '/many_cookies'           => ManyCookiesServlet,
  '/many_cookies_as_string' => ManyCookiesAsStringServlet,
  '/ntlm'                   => NTLMServlet,
  '/send_cookies'           => SendCookiesServlet,
  '/quoted_value_cookie'    => QuotedValueCookieServlet,
  '/if_modified_since'      => ModifiedSinceServlet,
  '/http_headers'           => HeaderServlet,
  '/infinite_redirect'      => InfiniteRedirectServlet,
  '/infinite_refresh'       => InfiniteRefreshServlet,
  '/redirect'               => RedirectServlet,
  '/refresh_without_url'    => RefreshWithoutUrl,
  '/refresh_with_empty_url' => RefreshWithEmptyUrl,
  '/digest_auth'            => DigestAuthServlet,
  '/verb'                   => VerbServlet,
}
PAGE_CACHE =
{}

Instance Method Summary collapse

Instance Method Details

#do_startObject



512
513
514
# File 'lib/mechanize/test_case.rb', line 512

def do_start
  @started = true
end

#keep_alive?(req, res) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
# File 'lib/mechanize/monkey_patch.rb', line 6

def keep_alive?(req, res)
  return false if /close/i =~ req['connection'].to_s
  return false if @seems_1_0_server
  return false if /close/i      =~ res['connection'].to_s
  return true  if /keep-alive/i =~ res['connection'].to_s
  return false if /close/i      =~ res['proxy-connection'].to_s
  return true  if /keep-alive/i =~ res['proxy-connection'].to_s
  (@curr_http_version == '1.1')
end

#old_do_startObject



510
# File 'lib/mechanize/test_case.rb', line 510

alias :old_do_start :do_start

#old_keep_alive?Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
12
# File 'lib/mechanize/monkey_patch.rb', line 4

def keep_alive?(req, res)
  return false if /close/i =~ req['connection'].to_s
  return false if @seems_1_0_server
  return false if /close/i      =~ res['connection'].to_s
  return true  if /keep-alive/i =~ res['connection'].to_s
  return false if /close/i      =~ res['proxy-connection'].to_s
  return true  if /keep-alive/i =~ res['proxy-connection'].to_s
  (@curr_http_version == '1.1')
end

#old_requestObject



546
# File 'lib/mechanize/test_case.rb', line 546

alias :old_request :request

#request(req, *data) {|response| ... } ⇒ Object

Yields:

  • (response)


548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'lib/mechanize/test_case.rb', line 548

def request(req, *data, &block)
  url = URI.parse(req.path)
  path = WEBrick::HTTPUtils.unescape(url.path)

  path = '/index.html' if path == '/'

  res = ::Response.new
  res.query_params = url.query

  req.query = if 'POST' != req.method && url.query then
                WEBrick::HTTPUtils.parse_query url.query
              elsif req['content-type'] =~ /www-form-urlencoded/ then
                WEBrick::HTTPUtils.parse_query req.body
              elsif req['content-type'] =~ /boundary=(.+)/ then
                boundary = WEBrick::HTTPUtils.dequote $1
                WEBrick::HTTPUtils.parse_form_data req.body, boundary
              else
                {}
              end

  req.cookies = WEBrick::Cookie.parse(req['Cookie'])

  Mechanize::TestCase::REQUESTS << req

  if servlet_klass = SERVLETS[path]
    servlet = servlet_klass.new({})
    servlet.send "do_#{req.method}", req, res
  else
    filename = "htdocs#{path.gsub(/[^\/\\.\w\s]/, '_')}"
    unless PAGE_CACHE[filename]
      open("#{Mechanize::TestCase::TEST_DIR}/#{filename}", 'rb') { |io|
        PAGE_CACHE[filename] = io.read
      }
    end

    res.body = PAGE_CACHE[filename]
    case filename
    when /\.txt$/
      res['Content-Type'] = 'text/plain'
    when /\.jpg$/
      res['Content-Type'] = 'image/jpeg'
    end
  end

  res['Content-Type'] ||= 'text/html'
  res.code ||= "200"

  response_klass = Net::HTTPResponse::CODE_TO_OBJ[res.code.to_s]
  response = response_klass.new res.http_version, res.code, res.message

  res.header.each do |k,v|
    v = v.first if v.length == 1
    response[k] = v
  end

  res.cookies.each do |cookie|
    response.add_field 'Set-Cookie', cookie.to_s
  end

  response['Content-Type'] ||= 'text/html'
  response['Content-Length'] = res['Content-Length'] || res.body.length.to_s

  io = StringIO.new(res.body)
  response.instance_variable_set :@socket, io
  def io.read clen, dest, _
    dest << string[0, clen]
  end

  body_exist = req.response_body_permitted? &&
    response_klass.body_permitted?

  response.instance_variable_set :@body_exist, body_exist

  yield response if block_given?

  response
end