处理异常状态
This commit is contained in:
parent
ce067a1e89
commit
e17c4d002c
@ -20,6 +20,7 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import fi.iki.elonen.NanoHTTPD;
|
||||
import fi.iki.elonen.NanoHTTPD.Response.Status;
|
||||
|
||||
public class NanoHTTPDWebserver extends NanoHTTPD {
|
||||
|
||||
@ -83,7 +84,7 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
|
||||
|
||||
private Response newFixedFileResponse(File file, String mime) throws FileNotFoundException {
|
||||
Response res;
|
||||
res = newFixedLengthResponse(Response.Status.OK, mime, new FileInputStream(file), (int) file.length());
|
||||
res = newFixedLengthResponse(Status.OK, mime, new FileInputStream(file), (int) file.length());
|
||||
res.addHeader("Accept-Ranges", "bytes");
|
||||
return res;
|
||||
}
|
||||
@ -132,7 +133,7 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
|
||||
// and the startFrom of the range is satisfiable
|
||||
// would return range from file
|
||||
// respond with not-modified
|
||||
res = newFixedLengthResponse(Response.Status.NOT_MODIFIED, mime, "");
|
||||
res = newFixedLengthResponse(Status.NOT_MODIFIED, mime, "");
|
||||
res.addHeader("ETag", etag);
|
||||
} else {
|
||||
if (endAt < 0) {
|
||||
@ -146,7 +147,7 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
fis.skip(startFrom);
|
||||
|
||||
res = newFixedLengthResponse(Response.Status.PARTIAL_CONTENT, mime, fis, newLen);
|
||||
res = newFixedLengthResponse(Status.PARTIAL_CONTENT, mime, fis, newLen);
|
||||
res.addHeader("Accept-Ranges", "bytes");
|
||||
res.addHeader("Content-Length", "" + newLen);
|
||||
res.addHeader("Content-Range", "bytes " + startFrom + "-" + endAt + "/" + fileLen);
|
||||
@ -157,21 +158,21 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
|
||||
if (headerIfRangeMissingOrMatching && range != null && startFrom >= fileLen) {
|
||||
// return the size of the file
|
||||
// 4xx responses are not trumped by if-none-match
|
||||
res = newFixedLengthResponse(Response.Status.RANGE_NOT_SATISFIABLE, NanoHTTPD.MIME_PLAINTEXT, "");
|
||||
res = newFixedLengthResponse(Status.RANGE_NOT_SATISFIABLE, NanoHTTPD.MIME_PLAINTEXT, "");
|
||||
res.addHeader("Content-Range", "bytes */" + fileLen);
|
||||
res.addHeader("ETag", etag);
|
||||
} else if (range == null && headerIfNoneMatchPresentAndMatching) {
|
||||
// full-file-fetch request
|
||||
// would return entire file
|
||||
// respond with not-modified
|
||||
res = newFixedLengthResponse(Response.Status.NOT_MODIFIED, mime, "");
|
||||
res = newFixedLengthResponse(Status.NOT_MODIFIED, mime, "");
|
||||
res.addHeader("ETag", etag);
|
||||
} else if (!headerIfRangeMissingOrMatching && headerIfNoneMatchPresentAndMatching) {
|
||||
// range request that doesn't match current etag
|
||||
// would return entire (different) file
|
||||
// respond with not-modified
|
||||
|
||||
res = newFixedLengthResponse(Response.Status.NOT_MODIFIED, mime, "");
|
||||
res = newFixedLengthResponse(Status.NOT_MODIFIED, mime, "");
|
||||
res.addHeader("ETag", etag);
|
||||
} else {
|
||||
// supply the file
|
||||
@ -181,7 +182,7 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
res = newFixedLengthResponse(Response.Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, ioe.getMessage());
|
||||
res = newFixedLengthResponse(Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, ioe.getMessage());
|
||||
}
|
||||
|
||||
return res;
|
||||
@ -250,8 +251,30 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
|
||||
return response;
|
||||
} else {
|
||||
try {
|
||||
response = newFixedLengthResponse(
|
||||
Response.Status.lookup(responseObject.getInt("status")),
|
||||
Response.IStatus status = Status.lookup(responseObject.getInt("status"));
|
||||
if(status == null){
|
||||
status = new Response.IStatus(){
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
try {
|
||||
return ""+this.getRequestStatus()+" "+responseObject.getString("body");
|
||||
} catch (JSONException e) {
|
||||
return ""+this.getRequestStatus()+" "+"Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRequestStatus() {
|
||||
try {
|
||||
return responseObject.getInt("status");
|
||||
} catch (JSONException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
response = newFixedLengthResponse(status,
|
||||
getContentType(responseObject),
|
||||
responseObject.getString("body")
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user