protected HttpResponse newHttpResponse(HttpMessageBuffer msg, HttpResponseException e) { HttpResponse httpRsp = e.toHttpResponse(msg.getHttpVersionString()); if (msg.getHttpVersion() > 1.0) { httpRsp.addHeader(HttpConstants.Headers.HOST, this.headerHostValue); } httpRsp.addHeader(HttpConstants.Headers.SERVER, this.getServerName()); httpRsp.addHeader(HttpConstants.Headers.CONNECTION, "close"); return httpRsp; }
protected HttpResponse newHttpResponse( HttpMessageBuffer origMsg, int statusCode, String reasonPhrase, Encoding encoding) { Encoding responseEncoding = null; if (this.encodeResponses) { responseEncoding = encoding; } HttpResponse httpRsp; if (origMsg == null) { httpRsp = new HttpResponse(statusCode, reasonPhrase, responseEncoding); } else { httpRsp = new HttpResponse( origMsg.getHttpVersionString(), statusCode, reasonPhrase, responseEncoding); } if (origMsg.getHttpVersion() > 1.0) { httpRsp.addHeader(HttpConstants.Headers.HOST, this.headerHostValue); } httpRsp.addHeader(HttpConstants.Headers.SERVER, this.getServerName()); return httpRsp; }
/** * Constructs a new {@link HttpResponse} containing the given XML-RPC method response. * * <p>This implementation encodes the response using <code>UTF-8</code>, sets the status code to * <code>200</code>, and sets <code>Content-Type</code> header to <code>text/xml</code> as * required. No other headers are set. * * @param methodRsp The XML-RPC method response to be returned in the HTTP response. * @param encoding An {@link Encoding} describing the encoding to use when constructing this * message. This also informs the <code>Content-Encoding</code> header value. May be <code> * null</code>. * @return A new {@link HttpResponse} with the marshalled XML-RPC response as its content. * @throws IOException if an error occurs while marshalling the XML-RPC response. * @throws MarshallingException */ protected HttpResponse toHttpResponse( HttpMessageBuffer origMsg, RpcResponse methodRsp, Encoding encoding) throws IOException, MarshallingException { ByteArrayOutputStream byteOs = new ByteArrayOutputStream(); methodRsp.marshal(byteOs, Charset.forName("UTF-8")); byte[] rspXml = byteOs.toByteArray(); HttpResponse httpRsp = this.newHttpResponse(origMsg, 200, "OK", encoding); httpRsp.addHeader(HttpConstants.Headers.CONTENT_TYPE, methodRsp.getContentType()); httpRsp.setContent(rspXml); return httpRsp; }
public HttpResponse getResponse(HttpRequestHeader hdr) { Log.i(LOG_TAG, " getResponse() started"); HttpResponse response; if (hdr.getHeaderParams() == null) { Log.e(LOG_TAG, "No header params found in request"); return new HttpResponse(HttpResponse.HTTP_BADREQUEST, "No header params found in request"); } String method = hdr.getHeaderParams().getProperty("method"); if (method == null) { Log.e(LOG_TAG, "No method in request headers"); return new HttpResponse(HttpResponse.HTTP_BADREQUEST, "No method in request headers"); } if (!method.equalsIgnoreCase("GET")) { Log.e(LOG_TAG, "getResponse() - only GET supported"); return new HttpResponse(HttpResponse.HTTP_NOTIMPLEMENTED, method + " not supported"); } String filename = hdr.getHeaderParams().getProperty("uri"); if (filename == null || filename.startsWith("..") || filename.endsWith("..") || filename.indexOf("../") >= 0) { Log.e(LOG_TAG, "getResponse() - file not found"); return new HttpResponse(HttpResponse.HTTP_FORBIDDEN, "Relative paths not allowed"); } try { String mime = ""; ContentInputStreamAdapter.ContentInputStream responseData = inputStreamAdapter.getContentInputStream(filename); if (responseData == null) { return new HttpResponse(HttpResponse.HTTP_NOTFOUND, "File not found"); } long startFrom = -1; long endAt = -1; String range = hdr.getHeaderParams().getProperty("range"); if (range != null) { Log.i(LOG_TAG, "Request contains a range: " + range); if (range.startsWith("bytes=")) { range = range.substring("bytes=".length()); int minus = range.indexOf('-'); try { if (minus > 0) { startFrom = Long.parseLong(range.substring(0, minus)); endAt = Long.parseLong(range.substring(minus + 1)); } } catch (NumberFormatException nfe) { Log.w(LOG_TAG, "Non Fatal Error extracting range :" + range, nfe); startFrom = -1; endAt = -1; } } } if (startFrom >= 0) { if (startFrom >= responseData.contentLength) { response = new HttpResponse( HttpResponse.HTTP_RANGE_NOT_SATISFIABLE, HttpResponse.MIME_PLAINTEXT, ""); response.addHeader("Content-Range", "bytes 0-0/" + responseData.contentLength); if (mime.startsWith("application/")) { response.addHeader( "Content-Disposition", "attachment; filename=\"" + responseData.filename + "\""); } } else { response = new HttpResponse( HttpResponse.HTTP_PARTIALCONTENT, responseData.mime, responseData.content); if (endAt < 0) { endAt = responseData.contentLength - 1; } long length = endAt - startFrom + 1; if (length < 0) { length = 0; } if (startFrom > 0) { responseData.content.skip(startFrom); } response.addHeader("Content-Length", "" + length); response.addHeader( "Content-Range", "bytes " + startFrom + "-" + endAt + "/" + responseData.contentLength); } } else { response = new HttpResponse(HttpResponse.HTTP_OK, responseData.mime, responseData.content); response.addHeader("Content-Length", "" + responseData.contentLength); response.addHeader("Accept-Ranges", "bytes"); } response.addHeader("Original-Length", "" + responseData.contentLength); response.addHeader( "Last-Modified", dateFormatter.format(new Date(responseData.lastModifiedDate))); } catch (IOException ioe) { response = new HttpResponse(HttpResponse.HTTP_INTERNALERROR, "IOException: " + ioe.getMessage()); Log.e(LOG_TAG, "Error Reading file", ioe); } response.addHeader("Date", dateFormatter.format(new Date())); response.addHeader("Content-Type", "audio/x-m4a"); response.addHeader("Connection", "close"); response.addHeader("Cache-Control", "no-cache"); response.addHeader("Expires", "-1"); response.addHeader("Pragma", "no-cache"); Log.i(LOG_TAG, " getResponse() ended. Response = " + response); return response; }