Class List Method List File List

# Class: Vertx::HttpServerResponse

Inherits:
  Object
  - Object
  - Vertx::HttpServerResponse
  show all
Includes:
  WriteStream
Defined in:
  src/main/ruby_scripts/core/http.rb

## Overview

Encapsulates a server-side HTTP response. An instance of this class is created and associated to every instance of HttpServerRequest that is created. It allows the developer to control the HTTP response that is sent back to the client for the corresponding HTTP request. It contains methods that allow HTTP headers and trailers to be set, and for a body to be written out to the response. It also allows a file to be streamed by the kernel directly from disk to the outgoing HTTP connection, bypassing user space altogether (where supported by the underlying operating system). This is a very efficient way of serving files from the server since buffers do not have to be read one by one from the file and written to the outgoing socket.

Author:

- Tim Fox

## Instance Method Summary (collapse)

- - (HttpServerResponse) chunked=(val)
  Sets whether this response uses HTTP chunked encoding or not.
- - (Object) close
  Close the underlying TCP connection.
- - (Object) end(data = nil)
  Ends the response.
- - (Hash) headers
  The response headers.
- - (HttpClientRequest) put_header(key, value)
  Inserts a header into the response.
- - (HttpClientRequest) put_trailer(key, value)
  Inserts a trailer into the response.
- - (HttpServerResponse) send_file(path)
  Tell the kernel to stream a file directly from disk to the outgoing connection, bypassing userspace altogether (where supported by the underlying operating system. This is a very efficient way to serve files..
- - (Object) status_code=(val)
  Set the status code of the response.
- - (Object) status_message=(val)
- - (Object) trailers
  The response trailers.
- - (HttpServerResponse) write_buffer(chunk, &hndlr)
  Write a buffer to the response.
- - (HttpServerResponse) write_str(str, enc = "UTF-8", &hndlr)
  Write a String to the response.

### Methods included from WriteStream

#drain_handler, #exception_handler, #write_queue_full?, #write_queue_max_size=

## Instance Method Details

### - (`HttpServerResponse`) chunked=(val)

Sets whether this response uses HTTP chunked encoding or not. will correspond to a new HTTP chunk sent on the wire. If chunked encoding is used the HTTP header 'Transfer-Encoding' with a value of 'Chunked' will be automatically inserted in the response. If chunked is false, this response will not use HTTP chunked encoding, and therefore if any data is written the body of the response, the total size of that data must be set in the 'Content-Length' header before any data is written to the response body. An HTTP chunked response is typically used when you do not know the total size of the request body up front.

Parameters:

- val. (`Boolean`) —
  If val is true, this response will use HTTP chunked encoding, and each call to write to the body

Returns:

- (`HttpServerResponse`) —
  self So multiple operations can be chained.

    647
    648
    649
    650
    # File 'src/main/ruby_scripts/core/http.rb', line 647

    def chunked=(val)
      @j_del.setChunked(val)
      self
    end

### - (`Object`) close

Close the underlying TCP connection

    665
    666
    667
    # File 'src/main/ruby_scripts/core/http.rb', line 665

    def close
      @j_del.close
    end

### - (`Object`) end(data = nil)

Ends the response. If no data has been written to the response body, the actual response won't get written until this method gets called. Once the response has ended, it cannot be used any more, and if keep alive is true the underlying connection will be closed.

Parameters:

- data. (`String`, `Buffer`) —
  Optional String or Buffer to write before ending the response

    656
    657
    658
    659
    660
    661
    662
    # File 'src/main/ruby_scripts/core/http.rb', line 656

    def end(data = nil)
      if (data.is_a? String) || (data.is_a? Buffer)
        @j_del.end(data)
      else
        @j_del.end
      end
    end

### - (`Hash`) headers

The response headers

Returns:

- (`Hash`) —
  The response headers

    576
    577
    578
    579
    580
    581
    # File 'src/main/ruby_scripts/core/http.rb', line 576

    def headers
      if !@headers
        @headers = @j_del.headers
      end
      @headers
    end

### - (`HttpClientRequest`) put_header(key, value)

Inserts a header into the response.

Parameters:

- key (`String`) —
  The header key
- value (`Object`) —
  The header value. to_s will be called on the value to determine the actual String value to insert.

Returns:

- (`HttpClientRequest`) —
  self So multiple operations can be chained.

    587
    588
    589
    590
    # File 'src/main/ruby_scripts/core/http.rb', line 587

    def put_header(key, value)
      @j_del.putHeader(key, value.to_s)
      self
    end

### - (`HttpClientRequest`) put_trailer(key, value)

Inserts a trailer into the response.

Parameters:

- key (`String`) —
  The header key
- value (`Object`) —
  The header value. to_s will be called on the value to determine the actual String value to insert.

Returns:

- (`HttpClientRequest`) —
  self So multiple operations can be chained.

    596
    597
    598
    599
    # File 'src/main/ruby_scripts/core/http.rb', line 596

    def put_trailer(key, value)
      @j_del.putTrailer(key, value.to_s)
      self
    end

### - (`HttpServerResponse`) send_file(path)

Tell the kernel to stream a file directly from disk to the outgoing connection, bypassing userspace altogether (where supported by the underlying operating system. This is a very efficient way to serve files.

Parameters:

- path. (`String`) —
  Path to file to send.

Returns:

- (`HttpServerResponse`) —
  self So multiple operations can be chained.

    633
    634
    635
    636
    # File 'src/main/ruby_scripts/core/http.rb', line 633

    def send_file(path)
      @j_del.sendFile(path)
      self
    end

### - (`Object`) status_code=(val)

Set the status code of the response. Default is 200

Parameters:

- val. (`FixNum`) —
  The HTTP status code.

    567
    568
    569
    # File 'src/main/ruby_scripts/core/http.rb', line 567

    def status_code=(val)
      @j_del.statusCode = val
    end

### - (`Object`) status_message=(val)

    571
    572
    573
    # File 'src/main/ruby_scripts/core/http.rb', line 571

    def status_message=(val)
      @j_del.statusMessage = val
    end

### - (`Object`) trailers

The response trailers

    602
    603
    604
    605
    606
    607
    # File 'src/main/ruby_scripts/core/http.rb', line 602

    def trailers
      if !@trailers
        @trailers = @j_del.trailers
      end
      @trailers
    end

### - (`HttpServerResponse`) write_buffer(chunk, &hndlr)

Write a buffer to the response. The handler will be called when the buffer has actually been written to the wire.

Parameters:

- chunk. (`Buffer`) —
  The buffer to write
- hndlr. (`Block`) —
  The handler

Returns:

- (`HttpServerResponse`) —
  self So multiple operations can be chained.

    613
    614
    615
    616
    # File 'src/main/ruby_scripts/core/http.rb', line 613

    def write_buffer(chunk, &hndlr)
      @j_del.writeBuffer(chunk._to_java_buffer)
      self
    end

### - (`HttpServerResponse`) write_str(str, enc = "UTF-8", &hndlr)

Write a String to the response. The handler will be called when the String has actually been written to the wire.

Parameters:

- str. (`String`) —
  The string to write
- enc. (`String`) —
  Encoding to use.
- hndlr. (`Block`) —
  The handler

Returns:

- (`HttpServerResponse`) —
  self So multiple operations can be chained.

    623
    624
    625
    626
    627
    # File 'src/main/ruby_scripts/core/http.rb', line 623

    def write_str(str, enc = "UTF-8", &hndlr)
      puts "writing str: #{str}"
      @j_del.write(str, enc)
      self
    end