📄 ajp13generator.java
字号:
addString(SERVER); } // TODO Add content length if last content known. // insert the number of headers int tmp = _buffer.putIndex(); _buffer.setPutIndex(field_index); addInt(num_fields); _buffer.setPutIndex(tmp); // get the payload size ( - 4 bytes for the ajp header) // excluding the // ajp header int payloadSize = _buffer.length() - 4; // insert the total packet size on 2nd and 3rd byte that // was previously // allocated addInt(2, payloadSize); } finally { _buffer = tmpbuf; } _state = STATE_CONTENT; } /* ------------------------------------------------------------ */ /** * Complete the message. * * @throws IOException */ public void complete() throws IOException { if (_state == STATE_END) return; super.complete(); if (_state < STATE_FLUSHING) { _state = STATE_FLUSHING; _needEOC = true; } flush(); } /* ------------------------------------------------------------ */ public long flush() throws IOException { try { if (_state == STATE_HEADER && !_expectMore) throw new IllegalStateException("State==HEADER"); prepareBuffers(); if (_endp == null) { // TODO - probably still needed! // if (_rneedMore && _buffe != null) // { // if(!_hasSentEOC) // _buffer.put(AJP13_MORE_CONTENT); // } if (!_expectMore && _needEOC && _buffer != null) { _buffer.put(AJP13_END_RESPONSE); } _needEOC = false; return 0; } // Keep flushing while there is something to flush // (except break below) int total = 0; long last_len = -1; Flushing: while (true) { int len = -1; int to_flush = ((_header != null && _header.length() > 0) ? 4 : 0) | ((_buffer != null && _buffer.length() > 0) ? 2 : 0); switch (to_flush) { case 7: throw new IllegalStateException(); // should // never // happen! case 6: len = _endp.flush(_header, _buffer, null); break; case 5: throw new IllegalStateException(); // should // never // happen! case 4: len = _endp.flush(_header); break; case 3: throw new IllegalStateException(); // should // never // happen! case 2: len = _endp.flush(_buffer); break; case 1: throw new IllegalStateException(); // should // never // happen! case 0: { // Nothing more we can write now. if (_header != null) _header.clear(); _bufferPrepared = false; if (_buffer != null) { _buffer.clear(); // reserve some space for the // header _buffer.setPutIndex(7); _buffer.setGetIndex(7); // Special case handling for // small left over buffer from // an addContent that caused a // buffer flush. if (_content != null && _content.length() < _buffer.space() && _state != STATE_FLUSHING) { _buffer.put(_content); _content.clear(); _content = null; break Flushing; } } // Are we completely finished for now? if (!_expectMore && !_needEOC && (_content == null || _content.length() == 0)) { if (_state == STATE_FLUSHING) _state = STATE_END;// if (_state == STATE_END)// {// _endp.close();// }// break Flushing; } // Try to prepare more to write. prepareBuffers(); } } // If we failed to flush anything twice in a row // break if (len <= 0) { if (last_len <= 0) break Flushing; break; } last_len = len; total += len; } return total; } catch (IOException e) { Log.ignore(e); throw (e instanceof EofException) ? e : new EofException(e); } } /* ------------------------------------------------------------ */ private void prepareBuffers() { if (!_bufferPrepared) { // Refill buffer if possible if (_content != null && _content.length() > 0 && _buffer != null && _buffer.space() > 0) { int len = _buffer.put(_content); // Make sure there is space for a trailing null if (len > 0 && _buffer.space() == 0) { len--; _buffer.setPutIndex(_buffer.putIndex() - 1); } _content.skip(len); if (_content.length() == 0) _content = null; if (_buffer.length() == 0) { _content = null; } } // add header if needed if (_buffer != null) { int payloadSize = _buffer.length(); // 4 bytes for the ajp header // 1 byte for response type // 2 bytes for the response size // 1 byte because we count from zero?? if (payloadSize > 0) { _bufferPrepared = true; _buffer.put((byte) 0); int put = _buffer.putIndex(); _buffer.setGetIndex(0); _buffer.setPutIndex(0); _buffer.put((byte) 'A'); _buffer.put((byte) 'B'); addInt(payloadSize + 4); _buffer.put((byte) 3); addInt(payloadSize); _buffer.setPutIndex(put); } } if (_needMore) { if (_header == null) { _header = _buffers.getBuffer(_headerBufferSize); } if (_buffer == null && _header != null && _header.space() >= AJP13_MORE_CONTENT.length) { _header.put(AJP13_MORE_CONTENT); _needMore = false; } else if (_buffer != null && _buffer.space() >= AJP13_MORE_CONTENT.length) { // send closing packet if all contents // are added _buffer.put(AJP13_MORE_CONTENT); _needMore = false; _bufferPrepared = true; } } if (!_expectMore && _needEOC) { if (_buffer == null && _header.space() >= AJP13_END_RESPONSE.length) { _header.put(AJP13_END_RESPONSE); _needEOC = false; } else if (_buffer != null && _buffer.space() >= AJP13_END_RESPONSE.length) { // send closing packet if all contents // are added _buffer.put(AJP13_END_RESPONSE); _needEOC = false; _bufferPrepared = true; } } } } /* ------------------------------------------------------------ */ public boolean isComplete() { return !_expectMore && _state == STATE_END; } /* ------------------------------------------------------------ */ private void initContent() throws IOException { if (_buffer == null) { _buffer = _buffers.getBuffer(_contentBufferSize); _buffer.setPutIndex(7); _buffer.setGetIndex(7); } } /* ------------------------------------------------------------ */ private void addInt(int i) { _buffer.put((byte) ((i >> 8) & 0xFF)); _buffer.put((byte) (i & 0xFF)); } /* ------------------------------------------------------------ */ private void addInt(int startIndex, int i) { _buffer.poke(startIndex, (byte) ((i >> 8) & 0xFF)); _buffer.poke((startIndex + 1), (byte) (i & 0xFF)); } /* ------------------------------------------------------------ */ private void addString(String str) { if (str == null) { addInt(0xFFFF); return; } // TODO - need to use a writer to convert, to avoid this hacky // conversion and temp buffer byte[] b = str.getBytes(); addInt(b.length); _buffer.put(b); _buffer.put((byte) 0); } /* ------------------------------------------------------------ */ private void addBuffer(Buffer b) { if (b == null) { addInt(0xFFFF); return; } addInt(b.length()); _buffer.put(b); _buffer.put((byte) 0); } /* ------------------------------------------------------------ */ public void getBodyChunk() throws IOException { _needMore = true; _expectMore = true; flush(); } /* ------------------------------------------------------------ */ public void gotBody() { _needMore = false; _expectMore = false; } /* ------------------------------------------------------------ */ public void sendCPong() throws IOException { Buffer buff = _buffers.getBuffer(AJP13_CPONG_RESPONSE.length); buff.put(AJP13_CPONG_RESPONSE); // flushing cpong response do { _endp.flush(buff); } while(buff.length() >0); _buffers.returnBuffer(buff); reset(true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -