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