1 28 29 package com.caucho.server.hmux; 30 31 import com.caucho.server.connection.AbstractHttpRequest; 32 import com.caucho.server.connection.AbstractHttpResponse; 33 import com.caucho.server.util.CauchoSystem; 34 import com.caucho.util.Alarm; 35 import com.caucho.util.CharBuffer; 36 import com.caucho.vfs.WriteStream; 37 38 import javax.servlet.http.Cookie ; 39 import java.io.IOException ; 40 41 45 public class HmuxResponse extends AbstractHttpResponse { 46 HmuxRequest _req; 47 48 HmuxResponse(HmuxRequest request) 49 { 50 super(request); 51 52 _req = request; 53 } 54 55 58 public boolean isTop() 59 { 60 if (! (_request instanceof AbstractHttpRequest)) 61 return false; 62 else { 63 return ((AbstractHttpRequest) _request).isTop(); 64 } 65 } 66 67 protected boolean writeHeadersInt(WriteStream os, int length) 68 throws IOException 69 { 70 CharBuffer cb = _cb; 71 cb.clear(); 72 cb.append((char) ((_statusCode / 100) % 10 + '0')); 73 cb.append((char) ((_statusCode / 10) % 10 + '0')); 74 cb.append((char) (_statusCode % 10 + '0')); 75 cb.append(' '); 76 cb.append(_statusMessage); 77 78 _req.writeStatus(cb); 79 80 if (_statusCode >= 400) { 81 removeHeader("ETag"); 82 removeHeader("Last-Modified"); 83 } 84 else if (_isNoCache) { 85 removeHeader("ETag"); 86 removeHeader("Last-Modified"); 87 88 setHeader("Expires", "Thu, 01 Dec 1994 16:00:00 GMT"); 89 _req.writeHeader("Cache-Control", "no-cache"); 90 } 91 else if (isPrivateCache()) 92 _req.writeHeader("Cache-Control", "private"); 93 94 int load = (int) (1000 * CauchoSystem.getLoadAvg()); 95 _req.writeString(HmuxRequest.HMUX_META_HEADER, "cpu-load"); 96 _req.writeString(HmuxRequest.HMUX_STRING, String.valueOf(load)); 97 98 for (int i = 0; i < _headerKeys.size(); i++) { 99 String key = (String ) _headerKeys.get(i); 100 String value = (String ) _headerValues.get(i); 101 102 _req.writeHeader(key, value); 103 } 104 105 if (_contentLength >= 0) { 106 cb.clear(); 107 cb.append(_contentLength); 108 _req.writeHeader("Content-Length", cb); 109 } 110 else if (length >= 0) { 111 cb.clear(); 112 cb.append(length); 113 _req.writeHeader("Content-Length", cb); 114 } 115 116 long now = Alarm.getCurrentTime(); 117 for (int i = 0; i < _cookiesOut.size(); i++) { 118 Cookie cookie = (Cookie ) _cookiesOut.get(i); 119 int cookieVersion = cookie.getVersion(); 120 121 fillCookie(cb, cookie, now, 0, false); 122 _req.writeHeader("Set-Cookie", cb); 123 if (cookieVersion > 0) { 124 fillCookie(cb, cookie, now, cookieVersion, true); 125 _req.writeHeader("Set-Cookie2", cb); 126 } 127 } 128 129 if (_contentType != null) { 130 if (_charEncoding != null) 131 _req.writeHeader("Content-Type", _contentType + "; charset=" + _charEncoding); 132 else 133 _req.writeHeader("Content-Type", _contentType); 134 135 } 136 137 _req.sendHeader(); 138 139 return false; 140 } 141 } 142 | Popular Tags |