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