import java.net.*;
import java.io.*;
import java.util.*;
/**
* Class ServerThread is the implementation
* of a thread of execution for the multithreaded
* Java HTTP server.
*/
class ServerThread implements Runnable {
/**
* String to represent the network end of line --
* the carriage return line feed.
*/
static private String CRLF = "\r\n";
/**
* Allow a max buffers size of 8192 bytes.
*/
static private int buffer_size = 8192;
/**
* Declare a thread object.
*/
Thread t;
/**
* Setup the current working directory as our document root
* directory.
*/
File root;
/**
* Each thread must have a socket associated with it.
*/
Socket socket;
/**
* Direct standard output to the screen.
*/
static PrintWriter screen = new PrintWriter (System.out, true);
/**
* When an instance of this class is created,
* the Thread t is started.
*/
public ServerThread(Socket s, File r) {
socket = s;
root = r;
t = new Thread(this, "Server Thread");
t.start();
}
/**
* Check if the requested file is at or below the document root
* directory.
*
* @param uri String string representing the requested file's uri.
* @return boolean
*/
private boolean isAllowed(String uri) {
/**
* Declare a new file object using the supplied uri.
*/
File f = new File(root, getUriFile(uri));
try {
/**
* Return whether or not the path of the request file starts with the root path.
*/
return (f.getCanonicalPath()).startsWith(root.getCanonicalPath());
} catch (IOException e) {
return false;
}
}
/**
* Builds a standard HTTP/1.0 response string.
*
* @param code int int representing the HTTP code
* @param msg String string representing the message for the response
* @param header String string representing the message header.
* @return String string representing the complete response message.
*/
private String makeResponse(int code, String msg, String header) {
return "HTTP/1.0" + " " + code + " " + msg + CRLF + header + CRLF;
}
/**
* Build the appropriate MIME string.
*
* @param type String string representing the MIME type.
* @param len int integer representing the message length
* @return String string representing the MIME portion of the message
*/
private String makeMIME(String type, int len) {
String mh =
"Server: SSD8Server/1.0" + CRLF +
"Content-type: " + type + CRLF +
"Content-length: " + len + CRLF;
return mh;
}
/**
* Generate a simple HTML document contain the necessary error codes to
* tell the client that a problem has occured.
*
* @param code int integer representing the error code to send
* @param msg string String representing the error message
* @return String containing the HTML document.
*/
private String error(int code, String msg) {
/**
* Build the html string.
*/
String html_page = "<HTML>" + "<BODY>" + CRLF +
"<H1>" + code + " " + msg + "</H1>" + "</BODY>" + "</HTML>" + CRLF;
/**
* Add the necessary MIME type to the document.
*/
String mh = makeMIME("text/html", html_page.length());
/**
* Add the appropriate response codes to the document.
*/
String hr = makeResponse(code, msg, mh);
/**
* Return the entire document.
*/
return hr + html_page;
}
/**
* Get the appropriate file type by matching suffixes --
* this is used to build the MIME types.
*
* @param filename String string representing the file name
* @return String string representing the MIME type
*/
private String getType(String filename) {
/**
* If the filename ends with .html, return
* a "text/html" MIME type.
*/
if (filename.endsWith(".html")) {
return "text/html";
} else if (filename.endsWith(".jpg")) {
/**
* If the filename ends with jpg, return a
* image/jpeg MIME type.
*/
return "image/jpeg";
/**
* By default return a "text/plain" MIME type.
*/
} else return "text/plain";
}
/**
* Take an absolute or relative URI, and return a string which points
* to the file relative to the server's root directory.
*
* @param uri String string representing the file's uri
* @return String representing the relative path.
*/
private String getUriFile(String uri) {
if (uri.startsWith("http://")) {
return uri.substring(uri.indexOf('/',8)+1);
} else if (uri.startsWith("/")) {
return uri.substring(1);
} else return uri;
}
/**
* handles a client's GET request
*
* @param ostream BufferedOutputStream stream of data written to the socket
* @param uri String string representing the uri
*/
private void handleGet(BufferedOutputStream ostream, String uri) {
try {
/**
* Build the filename and try to open the file. If
* an error occurs (if the file cannot be found), return
* an HTTP 404 error.
*/
String filename = getUriFile(uri);
File f = new File(root, filename);
if (!f.isFile()) {
ostream.write((error(404,"File not found")).getBytes());
} else {
/**
* Else, get the data file the file and send it with the appropriate
* response headers for HTTP and MIME.
*
* First, set the header and MIME type.
*/
String type = getType(f.getName());
String header = makeMIME(type,(int) f.length());
String response = makeResponse(200,"OK",header);
ostream.write(response.getBytes());
/**
* Now open the data file, read it, and send it back to the client.
*/
FileInputStream fstream = new FileInputStream(f);
int bufsize = 0;
/**
* Must use a byte array to handle images.
*/
byte[] buffer = new byte[buffer_size];
/**
* Loop while there is data to be read.
*/
while((bufsize = fstream.read(buffer)) != -1) {
ostream.write(buffer);
}
}
/**
* If an exception occurs, print
* out a stack trace.
*/
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Handle an HTTP PUT request.
*
* @param istream BufferedInputStream input stream from socket
* @param ostream BufferedOutputStream output stream to socket
* @param uri String string representing the file's uri
* @param len int length of input
*/
private void handlePut(BufferedInputStream istream,
BufferedOutputStream ostream,
String uri,
int len) {
try {
/**
* See if the file already exists in the root directory. If so, then
* simply return an OK (200) message. If you are not allowed to
* upload a file to this directory return a forbidden (403) message.
*/
String filename = getUriFile(uri);
File f = new File(root, filename);
String reply;
if (f.exists()) {
if (!f.isFile()) {
ostream.write((error(403,"Forbidden")).getBytes());
return;
} else {
reply = error(200, "OK");
}
} else {
/**
* If the file is uploadable and it is not
* already on the server, then cbuild a 201
* "created" message.
没有合适的资源?快使用搜索试试~ 我知道了~
http.rar_put
共4个文件
java:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 55 浏览量
2022-09-24
20:53:43
上传
评论
收藏 10KB RAR 举报
温馨提示
一个简单的http客户端和简单的http服务器,命令行输出和接收参数,并将返回内容存到本地。client可以实现向主机的get请求;服务器可以响应get、put请求
资源推荐
资源详情
资源评论
收起资源包目录
http.rar (4个子文件)
Server.java 13KB
HttpClient.java 3KB
ThreadedServer.java 14KB
Client.java 3KB
共 4 条
- 1
资源评论
小贝德罗
- 粉丝: 70
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功