Class: Vertx::HttpServerResponse

Inherits:
Object
  • Object
show all
Includes:
WriteStream
Defined in:
src/main/api_shim/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.

Author:

Instance Method Summary (collapse)

Methods included from WriteStream

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

Instance Method Details

- (Object) chunked(val = nil)

Get or set chunked


681
682
683
684
685
686
687
688
# File 'src/main/api_shim/core/http.rb', line 681

def chunked(val = nil)
  if val
    @j_del.setChunked(val)
    self
  else
    @j_del.getChunked
  end
end

- (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:



675
676
677
678
# File 'src/main/api_shim/core/http.rb', line 675

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

- (Object) close

Close the underlying TCP connection


760
761
762
# File 'src/main/api_shim/core/http.rb', line 760

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


751
752
753
754
755
756
757
# File 'src/main/api_shim/core/http.rb', line 751

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

- (MultiMap) headers

The response headers

Returns:



691
692
693
694
695
696
# File 'src/main/api_shim/core/http.rb', line 691

def headers
  if !@headers
    @headers = MultiMap.new(@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:



702
703
704
705
# File 'src/main/api_shim/core/http.rb', line 702

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:



711
712
713
714
# File 'src/main/api_shim/core/http.rb', line 711

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

- (HttpServerResponse) send_file(path, not_found_file = nil)

Tell the kernel to stream a file directly from disk to the outgoing connection, bypassing user-space 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.
  • not_found_file (String) (defaults to: nil)
    Path to file containing 404 resource in case resource can't be found

Returns:



738
739
740
741
742
743
744
745
# File 'src/main/api_shim/core/http.rb', line 738

def send_file(path, not_found_file = nil)
  if !not_found_file
    @j_del.sendFile(path)
  else
    @j_del.sendFile(path, not_found_file)
  end
  self
end

- (Object) status_code(val = nil)

Get or set the status code


642
643
644
645
646
647
648
649
# File 'src/main/api_shim/core/http.rb', line 642

def status_code(val = nil)
  if val
    @j_del.setStatusCode(val)
    self
  else
    @j_del.getStatusCode
  end
end

- (Object) status_code=(val)



637
638
639
# File 'src/main/api_shim/core/http.rb', line 637

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

- (Object) status_message(val = nil)

Get or set the status message


657
658
659
660
661
662
663
664
# File 'src/main/api_shim/core/http.rb', line 657

def status_message(val = nil)
  if val
    @j_del.setStatusMessage(val)
    self
  else
    @j_del.getStatusMessage
  end
end

- (Object) status_message=(val)

Set the status message


652
653
654
# File 'src/main/api_shim/core/http.rb', line 652

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

- (Object) trailers

The response trailers


717
718
719
720
721
722
# File 'src/main/api_shim/core/http.rb', line 717

def trailers
  if !@trailers
    @trailers = MultiMap.new(@j_del.trailers)
  end
  @trailers
end

- (HttpServerResponse) write_str(str, enc = "UTF-8")

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.

Returns:



728
729
730
731
# File 'src/main/api_shim/core/http.rb', line 728

def write_str(str, enc = "UTF-8")
  @j_del.write(str, enc)
  self
end