Home | History | Annotate | Download | only in spdy
      1 // Copyright (c) 2012 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 #ifndef NET_SPDY_SPDY_FRAME_READER_H_
      6 #define NET_SPDY_SPDY_FRAME_READER_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/strings/string_piece.h"
     10 #include "net/base/net_export.h"
     11 
     12 namespace net {
     13 
     14 // Used for reading SPDY frames. Though there isn't really anything terribly
     15 // SPDY-specific here, it's a helper class that's useful when doing SPDY
     16 // framing.
     17 //
     18 // To use, simply construct a SpdyFramerReader using the underlying buffer that
     19 // you'd like to read fields from, then call one of the Read*() methods to
     20 // actually do some reading.
     21 //
     22 // This class keeps an internal iterator to keep track of what's already been
     23 // read and each successive Read*() call automatically increments said iterator
     24 // on success. On failure, internal state of the SpdyFrameReader should not be
     25 // trusted and it is up to the caller to throw away the failed instance and
     26 // handle the error as appropriate. None of the Read*() methods should ever be
     27 // called after failure, as they will also fail immediately.
     28 class NET_EXPORT_PRIVATE SpdyFrameReader {
     29  public:
     30   // Caller must provide an underlying buffer to work on.
     31   SpdyFrameReader(const char* data, const size_t len);
     32 
     33   // Empty destructor.
     34   ~SpdyFrameReader() {}
     35 
     36   // Reads an 8-bit unsigned integer into the given output parameter.
     37   // Forwards the internal iterater on success.
     38   // Returns true on success, false otherwise.
     39   bool ReadUInt8(uint8* result);
     40 
     41   // Reads a 16-bit unsigned integer into the given output parameter.
     42   // Forwards the internal iterater on success.
     43   // Returns true on success, false otherwise.
     44   bool ReadUInt16(uint16* result);
     45 
     46   // Reads a 32-bit unsigned integer into the given output parameter.
     47   // Forwards the internal iterater on success.
     48   // Returns true on success, false otherwise.
     49   bool ReadUInt32(uint32* result);
     50 
     51   // Reads a 31-bit unsigned integer into the given output parameter. This is
     52   // equivalent to ReadUInt32() above except that the highest-order bit is
     53   // discarded.
     54   // Forwards the internal iterater (by 4B) on success.
     55   // Returns true on success, false otherwise.
     56   bool ReadUInt31(uint32* result);
     57 
     58   // Reads a 24-bit unsigned integer into the given output parameter.
     59   // Forwards the internal iterater (by 3B) on success.
     60   // Returns true on success, false otherwise.
     61   bool ReadUInt24(uint32* result);
     62 
     63   // Reads a string prefixed with 16-bit length into the given output parameter.
     64   //
     65   // NOTE: Does not copy but rather references strings in the underlying buffer.
     66   // This should be kept in mind when handling memory management!
     67   //
     68   // Forwards the internal iterater on success.
     69   // Returns true on success, false otherwise.
     70   bool ReadStringPiece16(base::StringPiece* result);
     71 
     72   // Reads a string prefixed with 32-bit length into the given output parameter.
     73   //
     74   // NOTE: Does not copy but rather references strings in the underlying buffer.
     75   // This should be kept in mind when handling memory management!
     76   //
     77   // Forwards the internal iterater on success.
     78   // Returns true on success, false otherwise.
     79   bool ReadStringPiece32(base::StringPiece* result);
     80 
     81   // Reads a given number of bytes into the given buffer. The buffer
     82   // must be of adequate size.
     83   // Forwards the internal iterater on success.
     84   // Returns true on success, false otherwise.
     85   bool ReadBytes(void* result, size_t size);
     86 
     87   // Seeks a given number of bytes into the buffer from the current offset.
     88   // Equivelant to an empty read.
     89   // Forwards the internal iterator.
     90   // Returns true on success, false otherwise.
     91   bool Seek(size_t size);
     92 
     93   // Rewinds this reader to the beginning of the frame.
     94   void Rewind() { ofs_ = 0; }
     95 
     96   // Returns true if the entirety of the underlying buffer has been read via
     97   // Read*() calls.
     98   bool IsDoneReading() const;
     99 
    100   // Returns the number of bytes that have been consumed by the reader so far.
    101   size_t GetBytesConsumed() const { return ofs_; }
    102 
    103  private:
    104   // Returns true if the underlying buffer has enough room to read the given
    105   // amount of bytes.
    106   bool CanRead(size_t bytes) const;
    107 
    108   // To be called when a read fails for any reason.
    109   void OnFailure();
    110 
    111   // The data buffer that we're reading from.
    112   const char* data_;
    113 
    114   // The length of the data buffer that we're reading from.
    115   const size_t len_;
    116 
    117   // The location of the next read from our data buffer.
    118   size_t ofs_;
    119 };
    120 
    121 }  // namespace net
    122 
    123 #endif  // NET_SPDY_SPDY_FRAME_READER_H_
    124