View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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  public class StreamContentParser extends ContentParser
28  {
29      private static final Logger LOG = Log.getLogger(StreamContentParser.class);
30  
31      private final FCGI.StreamType streamType;
32      private final Parser.Listener listener;
33      private State state = State.LENGTH;
34      private int contentLength;
35  
36      public StreamContentParser(HeaderParser headerParser, FCGI.StreamType streamType, Parser.Listener listener)
37      {
38          super(headerParser);
39          this.streamType = streamType;
40          this.listener = listener;
41      }
42  
43      @Override
44      public Result parse(ByteBuffer buffer)
45      {
46          while (buffer.hasRemaining())
47          {
48              switch (state)
49              {
50                  case LENGTH:
51                  {
52                      contentLength = getContentLength();
53                      state = State.CONTENT;
54                      break;
55                  }
56                  case CONTENT:
57                  {
58                      int length = Math.min(contentLength, buffer.remaining());
59                      int limit = buffer.limit();
60                      buffer.limit(buffer.position() + length);
61                      ByteBuffer slice = buffer.slice();
62                      buffer.position(buffer.limit());
63                      buffer.limit(limit);
64                      contentLength -= length;
65                      if (onContent(slice))
66                          return Result.ASYNC;
67                      if (contentLength > 0)
68                          break;
69                      state = State.LENGTH;
70                      return Result.COMPLETE;
71                  }
72                  default:
73                  {
74                      throw new IllegalStateException();
75                  }
76              }
77          }
78          return Result.PENDING;
79      }
80  
81      @Override
82      public void noContent()
83      {
84          try
85          {
86              listener.onEnd(getRequest());
87          }
88          catch (Throwable x)
89          {
90              if (LOG.isDebugEnabled())
91                  LOG.debug("Exception while invoking listener " + listener, x);
92          }
93      }
94  
95      protected boolean onContent(ByteBuffer buffer)
96      {
97          try
98          {
99              return listener.onContent(getRequest(), streamType, buffer);
100         }
101         catch (Throwable x)
102         {
103             if (LOG.isDebugEnabled())
104                 LOG.debug("Exception while invoking listener " + listener, x);
105             return false;
106         }
107     }
108 
109     protected void end(int request)
110     {
111     }
112 
113     private enum State
114     {
115         LENGTH, CONTENT
116     }
117 }