public class HPage
extends java.lang.Object
implements java.lang.Runnable, nxm.sys.inc.AsciiMap
| Modifier and Type | Class and Description |
|---|---|
static class |
HPage.MultipartData
The data associated with a multi-part request.
|
| Modifier and Type | Field and Description |
|---|---|
static byte[] |
EOL
HTTP header end-of-line (as bytes).
|
static java.lang.String |
EOLSTR
HTTP header end-of-line string (CR+LF).
|
HServer |
server
The server associated with the request.
|
BANG, BSLASH, BTIC, CARET, COLON, COMMA, CR, EQUALS, FTIC, HTAB, LBRACE, LCURLY, LF, LOWER2UPPER, LOWERA, LOWERZ, LPAREN, MINUS, NINE, NULL, PERIOD, PLUS, QUOTE, RBRACE, RCURLY, RPAREN, SCOLON, SLASH, SPACE, TILDE, UPPER2LOWER, UPPERA, UPPERZ, ZERO| Constructor and Description |
|---|
HPage(HServer server,
java.net.Socket sock) |
HPage(HServer server,
java.net.Socket sock,
java.lang.String scheme) |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the output socket.
|
void |
closeFromBody() |
void |
closeSocket()
Opens the output socket, same as close().
|
void |
closeWS() |
void |
dumpHeader() |
void |
flush()
Flush the output stream.
|
static boolean |
getDebug() |
java.lang.String |
getHeader(java.lang.String key) |
double |
getHeaderD(java.lang.String key) |
java.io.BufferedInputStream |
getInputStream()
Gets the input stream for reading from the socket connection.
|
java.lang.String |
getMethod()
Gets the HTTP request method used.
|
java.io.PrintStream |
getOutputStream()
Gets the output stream for writing to the socket connection.
|
nxm.sys.lib.Table |
getParameters()
Gets the set of parameters passed in on the URI.
|
HPage.MultipartData[] |
getPostData()
Gets the data associated with an HTTP POST.
|
HPage.MultipartData |
getPostData(java.lang.String name)
Gets the named data associated with an HTTP POST.
|
java.net.Socket |
getSocket() |
java.lang.String |
getURI()
Get the HTTP URI requested by client.
|
boolean |
isClosed() |
boolean |
isGetRequest()
Deprecated.
|
boolean |
isHeadRequest()
Deprecated.
|
boolean |
isNotRequest()
Deprecated.
|
boolean |
isOpened() |
void |
open()
Opens the output socket.
|
void |
openSocket()
Opens the output socket, same as open().
|
void |
openToBody(java.lang.String title) |
void |
openUpToBody(java.lang.String title) |
void |
openWS(java.lang.String protocol) |
int |
recvWS(byte[] b,
int off,
int bytes) |
void |
run() |
int |
sendWS(byte[] b,
int off,
int bytes) |
void |
setContentLength(long bytes)
Sets the content length of the response.
|
void |
setContentRange(long firstBytePos,
long lastBytePos,
long totalLength)
Sets the "Content-Range" of the response (HTTP 1.1 RFC 2616 section 14.16) for a response
to a Range (partial content) request.
|
void |
setContentType(java.lang.String type)
Sets the content type of the response.
|
void |
setDate(java.util.Date date)
Sets the date and time of the server in the response header.
|
static void |
setDebug(boolean val) |
void |
setLastModified(java.util.Date date)
Sets the last modified date and time of the response.
|
void |
setStatus(int status)
Sets the HTTP status.
|
int |
write(byte[] buf)
Writes data to the output socket.
|
int |
write(byte[] buf,
int off,
int len)
Writes data to the output socket.
|
void |
write(java.io.ByteArrayOutputStream baos) |
int |
write(java.lang.String line)
Writes text to the output socket.
|
void |
writeFile(java.lang.Object ref,
java.lang.Object fname)
Writes a file to the output socket.
|
int |
writeln(java.lang.String line)
Writes text to the output socket followed by an EOL (end-of-line).
|
void |
writeNotFound()
Convenient method to write "404 Not Found" HTML to output for requested URI.
|
public static final java.lang.String EOLSTR
public static final byte[] EOL
public final HServer server
public HPage(HServer server, java.net.Socket sock) throws java.io.IOException
java.io.IOExceptionpublic HPage(HServer server, java.net.Socket sock, java.lang.String scheme) throws java.io.IOException
java.io.IOExceptionpublic void run()
run in interface java.lang.Runnablepublic HPage.MultipartData[] getPostData()
public HPage.MultipartData getPostData(java.lang.String name)
Content-Disposition that is form-data with name="<name>".name - The name of the form-data entry to get.public java.lang.String getMethod()
public nxm.sys.lib.Table getParameters()
"http://localhost/" = { }
"http://localhost/?one=1" = {ONE="1"}
"http://localhost/?one=1&two=2" = {ONE="1",TWO="2"}
"http://localhost/?str=a+b+c" = {STR="a b c"}
For a POST request, this will search through the content passed and will merge in any
parameters specified with a MIME type of "application/x-url-encoded" or
"application/x-www-form-urlencoded". (If a Content-Disposition is specified for multi-part
data, a check is also made to verify that it is "form-data".) public void setStatus(int status)
Warning: HTTP 1.1 requires that "All 1xx (informational), 204 (no content), and
304 (not modified) responses MUST NOT include a message-body" (RFC 2616,
ftp://ftp.isi.edu/in-notes/rfc2616.txt).
If the status is set to one of these codes, the content length must be
set to 0. Failure to set the content length may cause strange errors on
some clients, including an occasional "java.net.SocketException:
Connection reset" exception on some Java clients.
status - One of the valid HTTP status codes from HttpURLConnection such as
HTTP_OK or HTTP_NOT_FOUND.public static void setDebug(boolean val)
public static boolean getDebug()
public java.lang.String getURI()
public boolean isOpened()
public boolean isClosed()
public void openSocket()
public void closeSocket()
public void open()
openSocket().public void close()
closeSocket().public void openToBody(java.lang.String title)
public void openUpToBody(java.lang.String title)
public void closeFromBody()
public void openWS(java.lang.String protocol)
public void closeWS()
public int recvWS(byte[] b,
int off,
int bytes)
public int sendWS(byte[] b,
int off,
int bytes)
public void flush()
public void setContentType(java.lang.String type)
type - The MIME type of the content.public void setContentLength(long bytes)
bytes - The content length in bytes.public void setContentRange(long firstBytePos,
long lastBytePos,
long totalLength)
firstBytePos - absolute offset of first byte position of rangelastBytePos - absolute offset of last byte position of range requesttotalLength - total number of bytes of media (can be negative if cannot be determined)public void setDate(java.util.Date date)
date - The last modified date and timepublic void setLastModified(java.util.Date date)
date - The last modified date and timepublic java.lang.String getHeader(java.lang.String key)
public void dumpHeader()
public double getHeaderD(java.lang.String key)
public int write(java.lang.String line)
line - The text to write.writeln(String)public int writeln(java.lang.String line)
line - The text to write.public void writeFile(java.lang.Object ref,
java.lang.Object fname)
(1) Open the named file
(2) Set the Content-Type (i.e. MIME type) of the response
(see BaseFile.getMimeType() and setContentType(java.lang.String))
(3) Set the Content-Length of the response in bytes
(see IOResource.getLength() and setContentLength(long))
(4) Open the socket (see open())
(5) Read the raw bytes of the file (see IOResource.read(byte[], int, int)) and
write them to the socket (see write(byte[],int,int))
(6) Close the socket (see close())
(7) Close the file
Note that this method assumes that the given file is the complete response to the given
request and handles it as such. Use of this method is entirely optional and users are
free to use the other methods in this class for writing out data.ref - The reference (usually a Command or Midas context).fname - The file name. The file must exist and be a static file that can be read for
for output over the socket. (Note that the file could have been generated in
response to this request and could even exist in RAM, it just needs to be
"static" at the point when this method is called.)public int write(byte[] buf)
write(buf, 0, buf.length).buf - The data buffer.public int write(byte[] buf,
int off,
int len)
buf - The data buffer.off - The offset into the buffer (usually 0).len - Number of bytes to write out (-1 -> len=buf.length).public void write(java.io.ByteArrayOutputStream baos)
public void writeNotFound()
setStatus(HttpURLConnection.HTTP_NOT_FOUND) and
open() methods SHOULD normally be called before this.public java.io.BufferedInputStream getInputStream()
public java.io.PrintStream getOutputStream()
PrintStream returned will be set to terminate all
lines with the CR + LF sequence, EOLSTR,
as is required for HTTP output streams.public java.net.Socket getSocket()
@Deprecated public boolean isHeadRequest()
@Deprecated public boolean isGetRequest()
@Deprecated public boolean isNotRequest()