View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.fcgi.parser;
20  
21  import java.nio.ByteBuffer;
22  
23  import org.eclipse.jetty.fcgi.FCGI;
24  import org.eclipse.jetty.util.log.Log;
25  import org.eclipse.jetty.util.log.Logger;
26  
27  /**
28   * <p>A stream content parser parses frame bodies of type STDIN, STDOUT and STDERR.</p>
29   * <p>STDOUT frame bodies are handled specially by {@link ResponseContentParser}.
30   */
31  public class StreamContentParser extends ContentParser
32  {
33      private static final Logger LOG = Log.getLogger(StreamContentParser.class);
34  
35      private final FCGI.StreamType streamType;
36      private final Parser.Listener listener;
37      private State state = State.LENGTH;
38      private int contentLength;
39  
40      public StreamContentParser(HeaderParser headerParser, FCGI.StreamType streamType, Parser.Listener listener)
41      {
42          super(headerParser);
43          this.streamType = streamType;
44          this.listener = listener;
45      }
46  
47      @Override
48      public Result parse(ByteBuffer buffer)
49      {
50          while (buffer.hasRemaining())
51          {
52              switch (state)
53              {
54                  case LENGTH:
55                  {
56                      contentLength = getContentLength();
57                      state = State.CONTENT;
58                      break;
59                  }
60                  case CONTENT:
61                  {
62                      int length = Math.min(contentLength, buffer.remaining());
63                      int limit = buffer.limit();
64                      buffer.limit(buffer.position() + length);
65                      ByteBuffer slice = buffer.slice();
66                      buffer.position(buffer.limit());
67                      buffer.limit(limit);
68                      contentLength -= length;
69                      if (onContent(slice))
70                          return Result.ASYNC;
71                      if (contentLength > 0)
72                          break;
73                      state = State.LENGTH;
74                      return Result.COMPLETE;
75                  }
76                  default:
77                  {
78                      throw new IllegalStateException();
79                  }
80              }
81          }
82          return Result.PENDING;
83      }
84  
85      @Override
86      public void noContent()
87      {
88          try
89          {
90              listener.onEnd(getRequest());
91          }
92          catch (Throwable x)
93          {
94              if (LOG.isDebugEnabled())
95                  LOG.debug("Exception while invoking listener " + listener, x);
96          }
97      }
98  
99      protected boolean onContent(ByteBuffer buffer)
100     {
101         try
102         {
103             return listener.onContent(getRequest(), streamType, buffer);
104         }
105         catch (Throwable x)
106         {
107             if (LOG.isDebugEnabled())
108                 LOG.debug("Exception while invoking listener " + listener, x);
109             return false;
110         }
111     }
112 
113     protected void end(int request)
114     {
115     }
116 
117     private enum State
118     {
119         LENGTH, CONTENT
120     }
121 }