-
Notifications
You must be signed in to change notification settings - Fork 136
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)-
urlis the URL of the host that you want to connect to (eg. "https://speedtest.fdossena.com") -
connectTimeoutandsoTimeoutare the timeouts for the socket in milliseconds. Set them to-1to let the system determine them -
recvBufferandsendBufferare sizes of the buffers used by the socket in bytes. Set them to-1to let the system determine them
public Connection(String url)-
urlis 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 togetOutputStreambut it returns aPrintStreaminstead, which is more convenient for writing UTF-8 strings -
getInputStreamReader(): similar togetInputStreambut it returns anInputStreamReaderinstead, 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. IfkeepAliveis set to true, it also sends aConnection: keep-aliveheader 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 requestedpathafter sending some data. An Exception is thrown if something goes wrong.-
keepAlive: if set to true, it will send theConnection: keep-aliveheader and the Connection can be reused -
contentType: if not null, it will send theContent-Typeheader with the specified content type -
contentLength: if >=0, it will send theContent-Lengthheader, and more data can be written to the socket's OutputStream
-
-
readLineUnbuffered(): reads a line from the socket's InputStream until\nis 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