Home | History | Annotate | Download | only in child
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 //
      5 // A delegate class of WebURLLoaderImpl that handles multipart/x-mixed-replace
      6 // data.  We special case multipart/x-mixed-replace because WebCore expects a
      7 // separate didReceiveResponse for each new message part.
      8 //
      9 // Most of the logic and edge case handling are based on the Mozilla's
     10 // implementation in netwerk/streamconv/converters/nsMultiMixedConv.cpp.
     11 // This seems like a derivative work, so here's the original license:
     12 
     13 /* ***** BEGIN LICENSE BLOCK *****
     14  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
     15  *
     16  * The contents of this file are subject to the Mozilla Public License Version
     17  * 1.1 (the "License"); you may not use this file except in compliance with
     18  * the License. You may obtain a copy of the License at
     19  * http://www.mozilla.org/MPL/
     20  *
     21  * Software distributed under the License is distributed on an "AS IS" basis,
     22  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
     23  * for the specific language governing rights and limitations under the
     24  * License.
     25  *
     26  * The Original Code is mozilla.org code.
     27  *
     28  * The Initial Developer of the Original Code is
     29  * Netscape Communications Corporation.
     30  * Portions created by the Initial Developer are Copyright (C) 1998
     31  * the Initial Developer. All Rights Reserved.
     32  *
     33  * Contributor(s):
     34  *
     35  * Alternatively, the contents of this file may be used under the terms of
     36  * either the GNU General Public License Version 2 or later (the "GPL"), or
     37  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
     38  * in which case the provisions of the GPL or the LGPL are applicable instead
     39  * of those above. If you wish to allow use of your version of this file only
     40  * under the terms of either the GPL or the LGPL, and not to allow others to
     41  * use your version of this file under the terms of the MPL, indicate your
     42  * decision by deleting the provisions above and replace them with the notice
     43  * and other provisions required by the GPL or the LGPL. If you do not delete
     44  * the provisions above, a recipient may use your version of this file under
     45  * the terms of any one of the MPL, the GPL or the LGPL.
     46  *
     47  * ***** END LICENSE BLOCK ***** */
     48 
     49 #ifndef WEBKIT_CHILD_MULTIPART_RESPONSE_DELEGATE_H_
     50 #define WEBKIT_CHILD_MULTIPART_RESPONSE_DELEGATE_H_
     51 
     52 #include <string>
     53 
     54 #include "base/basictypes.h"
     55 #include "third_party/WebKit/public/platform/WebURLResponse.h"
     56 #include "webkit/child/webkit_child_export.h"
     57 
     58 namespace blink {
     59 class WebURLLoader;
     60 class WebURLLoaderClient;
     61 }
     62 
     63 namespace content {
     64 // Used by unit tests to access private members.
     65 class MultipartResponseDelegateTester;
     66 }
     67 
     68 namespace webkit_glue {
     69 
     70 class WEBKIT_CHILD_EXPORT MultipartResponseDelegate {
     71  public:
     72   MultipartResponseDelegate(blink::WebURLLoaderClient* client,
     73                             blink::WebURLLoader* loader,
     74                             const blink::WebURLResponse& response,
     75                             const std::string& boundary);
     76 
     77   // Passed through from ResourceHandleInternal
     78   void OnReceivedData(const char* data, int data_len, int encoded_data_length);
     79   void OnCompletedRequest();
     80 
     81   // The request has been canceled, so stop making calls to the client.
     82   void Cancel() {
     83     client_ = NULL;
     84     loader_ = NULL;
     85   }
     86 
     87   // Returns the multi part boundary string from the Content-type header
     88   // in the response.
     89   // Returns true on success.
     90   static bool ReadMultipartBoundary(const blink::WebURLResponse& response,
     91                                     std::string* multipart_boundary);
     92 
     93   // Returns the lower and higher content ranges from an individual multipart
     94   // in a multipart response.
     95   // Returns true on success.
     96   static bool ReadContentRanges(
     97       const blink::WebURLResponse& response,
     98       int64* content_range_lower_bound,
     99       int64* content_range_upper_bound,
    100       int64* content_range_instance_size);
    101 
    102  private:
    103   friend class content::MultipartResponseDelegateTester;  // For unittests.
    104 
    105   // Pointers to the client and associated loader so we can make callbacks as
    106   // we parse pieces of data.
    107   blink::WebURLLoaderClient* client_;
    108   blink::WebURLLoader* loader_;
    109 
    110   // The original resource response for this request.  We use this as a
    111   // starting point for each parts response.
    112   blink::WebURLResponse original_response_;
    113 
    114   // Checks to see if data[pos] character is a line break; handles crlf, lflf,
    115   // lf, or cr. Returns the number of characters to skip over (0, 1 or 2).
    116   int PushOverLine(const std::string& data, size_t pos);
    117 
    118   // Tries to parse http headers from the start of data_.  Returns true if it
    119   // succeeds and sends a didReceiveResponse to m_client.  Returns false if
    120   // the header is incomplete (in which case we just wait for more data).
    121   bool ParseHeaders();
    122 
    123   // Find the next boundary in data_.  Returns std::string::npos if there's no
    124   // full token.
    125   size_t FindBoundary();
    126 
    127   // Transferred data size accumulated between client callbacks.
    128   int encoded_data_length_;
    129 
    130   // A temporary buffer to hold data between reads for multipart data that
    131   // gets split in the middle of a header.
    132   std::string data_;
    133 
    134   // Multipart boundary token
    135   std::string boundary_;
    136 
    137   // true until we get our first on received data call
    138   bool first_received_data_;
    139 
    140   // true if we're truncated in the middle of a header
    141   bool processing_headers_;
    142 
    143   // true when we're done sending information.  At that point, we stop
    144   // processing AddData requests.
    145   bool stop_sending_;
    146 
    147   // true after we've sent our first response to the WebURLLoaderClient.
    148   bool has_sent_first_response_;
    149 
    150   DISALLOW_COPY_AND_ASSIGN(MultipartResponseDelegate);
    151 };
    152 
    153 }  // namespace webkit_glue
    154 
    155 #endif  // WEBKIT_CHILD_MULTIPART_RESPONSE_DELEGATE_H_
    156