Home | History | Annotate | Download | only in flip_server
      1 // Copyright (c) 2009 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_TOOLS_FLIP_SERVER_MEM_CACHE_H_
      6 #define NET_TOOLS_FLIP_SERVER_MEM_CACHE_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "net/tools/flip_server/balsa_headers.h"
     13 #include "net/tools/flip_server/balsa_visitor_interface.h"
     14 #include "net/tools/flip_server/constants.h"
     15 
     16 namespace net {
     17 
     18 class StoreBodyAndHeadersVisitor: public BalsaVisitorInterface {
     19  public:
     20   void HandleError() { error_ = true; }
     21 
     22   // BalsaVisitorInterface:
     23   virtual void ProcessBodyInput(const char *input, size_t size) {}
     24   virtual void ProcessBodyData(const char *input, size_t size);
     25   virtual void ProcessHeaderInput(const char *input, size_t size) {}
     26   virtual void ProcessTrailerInput(const char *input, size_t size) {}
     27   virtual void ProcessHeaders(const BalsaHeaders& headers) {
     28     // nothing to do here-- we're assuming that the BalsaFrame has
     29     // been handed our headers.
     30   }
     31   virtual void ProcessRequestFirstLine(const char* line_input,
     32                                        size_t line_length,
     33                                        const char* method_input,
     34                                        size_t method_length,
     35                                        const char* request_uri_input,
     36                                        size_t request_uri_length,
     37                                        const char* version_input,
     38                                        size_t version_length) {}
     39   virtual void ProcessResponseFirstLine(const char *line_input,
     40                                         size_t line_length,
     41                                         const char *version_input,
     42                                         size_t version_length,
     43                                         const char *status_input,
     44                                         size_t status_length,
     45                                         const char *reason_input,
     46                                         size_t reason_length) {}
     47   virtual void ProcessChunkLength(size_t chunk_length) {}
     48   virtual void ProcessChunkExtensions(const char *input, size_t size) {}
     49   virtual void HeaderDone() {}
     50   virtual void MessageDone() {}
     51   virtual void HandleHeaderError(BalsaFrame* framer);
     52   virtual void HandleHeaderWarning(BalsaFrame* framer);
     53   virtual void HandleChunkingError(BalsaFrame* framer);
     54   virtual void HandleBodyError(BalsaFrame* framer);
     55 
     56   BalsaHeaders headers;
     57   std::string body;
     58   bool error_;
     59 };
     60 
     61 ////////////////////////////////////////////////////////////////////////////////
     62 
     63 struct FileData {
     64   FileData();
     65   FileData(BalsaHeaders* h, const std::string& b);
     66   ~FileData();
     67   void CopyFrom(const FileData& file_data);
     68 
     69   BalsaHeaders* headers;
     70   std::string filename;
     71   // priority, filename
     72   std::vector< std::pair<int, std::string> > related_files;
     73   std::string body;
     74 };
     75 
     76 ////////////////////////////////////////////////////////////////////////////////
     77 
     78 class MemCacheIter {
     79  public:
     80   MemCacheIter() :
     81       file_data(NULL),
     82       priority(0),
     83       transformed_header(false),
     84       body_bytes_consumed(0),
     85       stream_id(0),
     86       max_segment_size(kInitialDataSendersThreshold),
     87       bytes_sent(0) {}
     88   explicit MemCacheIter(FileData* fd) :
     89       file_data(fd),
     90       priority(0),
     91       transformed_header(false),
     92       body_bytes_consumed(0),
     93       stream_id(0),
     94       max_segment_size(kInitialDataSendersThreshold),
     95       bytes_sent(0) {}
     96   FileData* file_data;
     97   int priority;
     98   bool transformed_header;
     99   size_t body_bytes_consumed;
    100   uint32 stream_id;
    101   uint32 max_segment_size;
    102   size_t bytes_sent;
    103 };
    104 
    105 ////////////////////////////////////////////////////////////////////////////////
    106 
    107 class MemoryCache {
    108  public:
    109   typedef std::map<std::string, FileData> Files;
    110 
    111  public:
    112   MemoryCache();
    113   ~MemoryCache();
    114 
    115   void CloneFrom(const MemoryCache& mc);
    116 
    117   void AddFiles();
    118 
    119   void ReadToString(const char* filename, std::string* output);
    120 
    121   void ReadAndStoreFileContents(const char* filename);
    122 
    123   FileData* GetFileData(const std::string& filename);
    124 
    125   bool AssignFileData(const std::string& filename, MemCacheIter* mci);
    126 
    127   Files files_;
    128   std::string cwd_;
    129 };
    130 
    131 class NotifierInterface {
    132  public:
    133   virtual ~NotifierInterface() {}
    134   virtual void Notify() = 0;
    135 };
    136 
    137 }  // namespace net
    138 
    139 #endif  // NET_TOOLS_FLIP_SERVER_MEM_CACHE_H_
    140 
    141