public void setContent(byte[] cntnt) throws Exception { StringBuffer sb = new StringBuffer(isHTTP11 ? "HTTP/1.1" : "HTTP/1.0"); sb.append(OK_HDR); sb.append("Content-Type: "); sb.append(contentType); if (!isHTTP11 || !keepAlive || isMessages) { sb.append("\r\nConnection: close\r\nProxy-Connection: close"); } else { sb.append("\r\nConnection: Keep-Alive\r\nProxy-Connection: Keep-Alive"); sb.append("\r\nContent-Length: "); sb.append(cntnt.length); } sb.append("\r\n\r\n"); sb.trimToSize(); CharBuffer cb = CharBuffer.wrap(sb.toString()); ByteBuffer tempBuffer; try { tempBuffer = Charset.forName("iso-8859-1").newEncoder().encode(cb); } catch (CharacterCodingException cce) { Server.debug(this, "prepareForSending: ", cce, Server.MSG_ERROR, Server.LVL_MINOR); try { tempBuffer = ByteBuffer.wrap(cb.toString().getBytes(Server.srv.DEFAULT_CHARSET)); } catch (Exception e) { Server.debug(this, "prepareForSending: ", e, Server.MSG_ERROR, Server.LVL_MAJOR); throw e; } } this.buf = ByteBuffer.allocate(tempBuffer.capacity() + cntnt.length); this.buf.put(tempBuffer.array()); this.buf.put(cntnt); this.buf.flip(); }
/** renders the template and wraps it to a full httpresponse */ public void renderTemplate(IRequest req) { if (tpl == null) { if (this.ts == null) ts = Server.srv.templatemanager.getTemplateSet("default"); tpl = ts.getTemplate(tName); if (tpl == null) tpl = ts.getTemplate("not_found"); } if (tpl.isRedirect()) { this.setRedirectTo(tpl.getDestination(), req.getCookieDomain()); return; } if (!nocache && !nostore && !tpl.hasToBeRendered( req.getProperty("if-none-match"), HttpDateParser.parseDate(req.getProperty("if-modified-since")))) { StringBuffer sb = new StringBuffer(isHTTP11 ? "HTTP/1.1" : "HTTP/1.0"); sb.append(IResponseHeaders.NOT_MODIFIED); sb.trimToSize(); prepareForSending(CharBuffer.wrap(sb.toString())); return; } String cntnt = tpl.render(req); if (cntnt == null || cntnt.length() < 1) { Server.log( this, "renderTemplate: rendered template has no content", Server.MSG_STATE, Server.LVL_MAJOR); resCode = NOCONTENT_CODE; StringBuffer sb = new StringBuffer(); sb.append("<html><body><b>The requested page could not be displayed!<br><br>Reason:</b> "); if (tpl == null) { sb.append("No template given"); } else { sb.append("Template '"); sb.append(tpl.getName()); sb.append("' has not been found on this server."); } sb.append("</body></html>"); wrap(sb.toString(), req.getCookieDomain()); return; } nocache = tpl.notCacheable(); // if (nocache) // Server.log (this, "not cacheable", Server.MSG_STATE, Server.LVL_VERY_VERBOSE); wrap(cntnt, tpl.getEtag(), req.getCookieDomain()); }
public void wrap(String cntnt, String eTag, String CookieDomain) { StringBuffer sb = new StringBuffer(isHTTP11 ? "HTTP/1.1" : "HTTP/1.0"); switch (resCode) { case OK_CODE: sb.append(OK_HDR); break; case REDIRECT_CODE: setRedirectTo(cntnt, CookieDomain); break; case NOCONTENT_CODE: sb.append(NOCONTENT_HDR); prepareForSending(CharBuffer.wrap(sb.toString())); return; case AUTHENTICATE_CODE: sb.append(AUTHENTICATE_HDR); contentType = "text/html"; break; case NOTFOUND_CODE: sb.append(NOTFOUND_HDR); contentType = "text/html"; break; } sb.append("Content-Type: "); sb.append(contentType); sb.append("; charset="); sb.append(Server.srv.DEFAULT_CHARSET); if (nocache) { sb.append("\r\nPragma: no-cache\r\nCache-Control: no-cache"); sb.append("\r\nExpires: Thu, 01 Dec 1994 16:00:00 GMT"); } if (nostore) { sb.append("\r\nCache-Control: no-store"); } if (eTag != null) { sb.append("\r\nETag: \"").append(eTag).append("\""); // Server.log (this, "sending eTag: " + eTag, Server.MSG_STATE, Server.LVL_MAJOR); } sb = appendCookie(sb, CookieDomain); if (!isHTTP11 || !keepAlive || isMessages) { sb.append("\r\nConnection: close\r\nProxy-Connection: close"); } else { sb.append("\r\nConnection: Keep-Alive\r\nProxy-Connection: Keep-Alive"); if (!chunkedHdr) { sb.append("\r\nContent-Length: "); sb.append(cntnt.length()); } } if (chunkedHdr) { sb.append("\r\nTransfer-Encoding: chunked\r\n\r\n"); sb.append(Integer.toHexString(cntnt.length())); sb.append("\r\n"); sb.append(cntnt); sb.append("\r\n"); sb.trimToSize(); prepareForSending(CharBuffer.wrap(sb.toString())); return; } sb.append("\r\n\r\n"); if ("iso-8859-1".equals(Server.srv.DEFAULT_CHARSET)) { sb.append(cntnt); sb.trimToSize(); prepareForSending(CharBuffer.wrap(sb.toString())); } else { sb.trimToSize(); CharBuffer hdrChar = CharBuffer.wrap(sb.toString()); prepareForSending(hdrChar, CharBuffer.wrap(cntnt)); } }
/** * construct a HTTP-Redirect-Response * * @param dest the destination to redirect to */ public void setRedirectTo(String dest, String CookieDomain) { StringBuffer cntnt = new StringBuffer( "<html><head><title>redirection</title><head><body>Redirected to <a href=\""); cntnt.append(dest); cntnt.append("\">"); cntnt.append(dest); cntnt.append("</a>"); cntnt.append("</body></html>"); int len = cntnt.length(); StringBuffer sb = new StringBuffer(isHTTP11 ? "HTTP/1.1" : "HTTP/1.0"); sb.append(REDIRECT_HDR); sb.append(Server.srv.DEFAULT_CHARSET); sb.append("\r\nLocation: "); sb.append(dest); sb.append("\r\nContent-Length: "); sb.append(len); sb = appendCookie(sb, CookieDomain); sb.append("\r\n\r\n"); if ("iso-8859-1".equals(Server.srv.DEFAULT_CHARSET)) { sb.append(cntnt); sb.trimToSize(); prepareForSending(CharBuffer.wrap(sb.toString())); } else { CharBuffer hdrChar = CharBuffer.wrap(sb.toString()); cntnt.trimToSize(); prepareForSending(hdrChar, CharBuffer.wrap(cntnt)); } isRedirect = true; }
public boolean canUseTemplateset(TemplateSet t) { if (Server.srv.DEFAULT_TEMPLATESET == null || t == null) return true; StringBuffer defaultTs = new StringBuffer(Server.srv.DEFAULT_TEMPLATESET); if (defaultTs != null) { StringTokenizer st = new StringTokenizer(defaultTs.toString(), ","); while (st.hasMoreTokens()) { StringBuilder templateName = new StringBuilder(st.nextToken()); if (templateName.toString().equals(t.getName())) return true; } } if (Server.DEBUG) { StringBuffer scn = new StringBuffer(); scn.append(" can not use Template "); scn.append(t.getName()); Server.log(this, scn.toString(), Server.MSG_TRAFFIC, Server.LVL_MINOR); } return false; }
/** * append the cookie-header-field to the given StringBuffer * * @param sb the stringbuffer to append the cookie-header-field * @return the stringbuffer with the cookie-header-field appended */ public StringBuffer appendCookie(StringBuffer sb, String CookieDomain) { if (cookie == null || CookieDomain == null) return sb; sb.append("\r\n"); sb.append("Set-Cookie: FreeCSSession="); sb.append(cookie); sb.append("; path=/;"); if (Server.srv.COOKIE_DOMAIN != null) { if (Server.DEBUG) Server.log( "[ContenCointainer]", "append FreecsSession = " + CookieDomain, Server.MSG_TRAFFIC, Server.LVL_VERY_VERBOSE); sb.append(" Domain="); sb.append(CookieDomain); } return sb; }