-
Notifications
You must be signed in to change notification settings - Fork 127
base package
Federico Dossena edited this page Sep 12, 2019
·
2 revisions
This class provides some static functions used throughout the test. These functions are:
-
urlEncode(String s)
: URL-encodes a string (UTF-8) -
sleep(long ms)
andsleep(long ms, int ns)
: simple sleep functions -
url_sep(String url)
: Returns the proper separator to use for GET parameters for a given string (eg. for "https://example.com/index.php" it will return "?", for "https://example.com/?id=whatever" it will return "&")
This is the foundation on which relies most of the test.
It uses a Socket to connect to the HTTP server, using either HTTP or HTTPS, and provides several methods to interact with it.
Constructors:
public Connection(String url, int connectTimeout, int soTimeout, int recvBuffer, int sendBuffer)
-
url
is the URL of the host that you want to connect to (eg. "https://speedtest.fdossena.com") -
connectTimeout
andsoTimeout
are the timeouts for the socket in milliseconds. Set them to-1
to let the system determine them -
recvBuffer
andsendBuffer
are sizes of the buffers used by the socket in bytes. Set them to-1
to let the system determine them
public Connection(String url)
-
url
is the URL of the host that you want to connect to (eg. "https://speedtest.fdossena.com")
If you use this constructor, it will use a connectTimeout
of 2000
, a soTimeout
of 5000
and default buffer sizes.
Note: URLs can be in one of these formats:
-
http://address
: if the server only supports HTTP -
https://address
: if the server only supports HTTPS -
//address
: if the server supports both HTTP and HTTPS (HTTPS preferred)
Important: This class DOES NOT handle HTTP redirects (3xx codes)!
Methods:
-
getInputStream()
: returns the socket's InputStream or null if the socket is closed/dead -
getOutputStream()
: returns the socket's OutputStream or null if the socket is closed/dead -
getPrintStream()
: similar togetOutputStream
but it returns aPrintStream
instead, which is more convenient for writing UTF-8 strings -
getInputStreamReader()
: similar togetInputStream
but it returns anInputStreamReader
instead, which is more convenient for reading UTF-8 strings -
GET(String path, boolean keepAlive)
: writes a GET request on the socket to fetch the resource at the requestedpath
. IfkeepAlive
is set to true, it also sends aConnection: keep-alive
header so that the Connection can be reused. An Exception is thrown if something goes wrong. -
POST(String path, boolean keepAlive, String contentType, long contentLength):
: writes a POST request on the socket to fetch the requestedpath
after sending some data. An Exception is thrown if something goes wrong.-
keepAlive
: if set to true, it will send theConnection: keep-alive
header and the Connection can be reused -
contentType
: if not null, it will send theContent-Type
header with the specified content type -
contentLength
: if >=0
, it will send theContent-Length
header, and more data can be written to the socket's OutputStream
-
-
readLineUnbuffered()
: reads a line from the socket's InputStream until\n
is encountered -
parseResponseHeaders()
: reads an entire HTTP response from the socket's InputStream. The headers are returned in aHashmap<String,String>
. Throws an exception if something goes wrong or if the response was not a200 OK
-
close()
: closes the socket