Home | History | Annotate | Download | only in server
      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_SERVER_HTTP_CONNECTION_H_
      6 #define NET_SERVER_HTTP_CONNECTION_H_
      7 
      8 #include <queue>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "net/base/io_buffer.h"
     15 
     16 namespace net {
     17 
     18 class StreamSocket;
     19 class WebSocket;
     20 
     21 // A container which has all information of an http connection. It includes
     22 // id, underlying socket, and pending read/write data.
     23 class HttpConnection {
     24  public:
     25   // IOBuffer for data read.  It's a wrapper around GrowableIOBuffer, with more
     26   // functions for buffer management.  It moves unconsumed data to the start of
     27   // buffer.
     28   class ReadIOBuffer : public IOBuffer {
     29    public:
     30     static const int kInitialBufSize = 1024;
     31     static const int kMinimumBufSize = 128;
     32     static const int kCapacityIncreaseFactor = 2;
     33     static const int kDefaultMaxBufferSize = 1 * 1024 * 1024;  // 1 Mbytes.
     34 
     35     ReadIOBuffer();
     36 
     37     // Capacity.
     38     int GetCapacity() const;
     39     void SetCapacity(int capacity);
     40     // Increases capacity and returns true if capacity is not beyond the limit.
     41     bool IncreaseCapacity();
     42 
     43     // Start of read data.
     44     char* StartOfBuffer() const;
     45     // Returns the bytes of read data.
     46     int GetSize() const;
     47     // More read data was appended.
     48     void DidRead(int bytes);
     49     // Capacity for which more read data can be appended.
     50     int RemainingCapacity() const;
     51 
     52     // Removes consumed data and moves unconsumed data to the start of buffer.
     53     void DidConsume(int bytes);
     54 
     55     // Limit of how much internal capacity can increase.
     56     int max_buffer_size() const { return max_buffer_size_; }
     57     void set_max_buffer_size(int max_buffer_size) {
     58       max_buffer_size_ = max_buffer_size;
     59     }
     60 
     61    private:
     62     virtual ~ReadIOBuffer();
     63 
     64     scoped_refptr<GrowableIOBuffer> base_;
     65     int max_buffer_size_;
     66 
     67     DISALLOW_COPY_AND_ASSIGN(ReadIOBuffer);
     68   };
     69 
     70   // IOBuffer of pending data to write which has a queue of pending data. Each
     71   // pending data is stored in std::string.  data() is the data of first
     72   // std::string stored.
     73   class QueuedWriteIOBuffer : public IOBuffer {
     74    public:
     75     static const int kDefaultMaxBufferSize = 1 * 1024 * 1024;  // 1 Mbytes.
     76 
     77     QueuedWriteIOBuffer();
     78 
     79     // Whether or not pending data exists.
     80     bool IsEmpty() const;
     81 
     82     // Appends new pending data and returns true if total size doesn't exceed
     83     // the limit, |total_size_limit_|.  It would change data() if new data is
     84     // the first pending data.
     85     bool Append(const std::string& data);
     86 
     87     // Consumes data and changes data() accordingly.  It cannot be more than
     88     // GetSizeToWrite().
     89     void DidConsume(int size);
     90 
     91     // Gets size of data to write this time. It is NOT total data size.
     92     int GetSizeToWrite() const;
     93 
     94     // Total size of all pending data.
     95     int total_size() const { return total_size_; }
     96 
     97     // Limit of how much data can be pending.
     98     int max_buffer_size() const { return max_buffer_size_; }
     99     void set_max_buffer_size(int max_buffer_size) {
    100       max_buffer_size_ = max_buffer_size;
    101     }
    102 
    103    private:
    104     virtual ~QueuedWriteIOBuffer();
    105 
    106     std::queue<std::string> pending_data_;
    107     int total_size_;
    108     int max_buffer_size_;
    109 
    110     DISALLOW_COPY_AND_ASSIGN(QueuedWriteIOBuffer);
    111   };
    112 
    113   HttpConnection(int id, scoped_ptr<StreamSocket> socket);
    114   ~HttpConnection();
    115 
    116   int id() const { return id_; }
    117   StreamSocket* socket() const { return socket_.get(); }
    118   ReadIOBuffer* read_buf() const { return read_buf_.get(); }
    119   QueuedWriteIOBuffer* write_buf() const { return write_buf_.get(); }
    120 
    121   WebSocket* web_socket() const { return web_socket_.get(); }
    122   void SetWebSocket(scoped_ptr<WebSocket> web_socket);
    123 
    124  private:
    125   const int id_;
    126   const scoped_ptr<StreamSocket> socket_;
    127   const scoped_refptr<ReadIOBuffer> read_buf_;
    128   const scoped_refptr<QueuedWriteIOBuffer> write_buf_;
    129 
    130   scoped_ptr<WebSocket> web_socket_;
    131 
    132   DISALLOW_COPY_AND_ASSIGN(HttpConnection);
    133 };
    134 
    135 }  // namespace net
    136 
    137 #endif  // NET_SERVER_HTTP_CONNECTION_H_
    138