1// Copyright 2013 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 LIBRARIES_NACL_IO_HTTPFS_HTTP_FS_NODE_H_
6#define LIBRARIES_NACL_IO_HTTPFS_HTTP_FS_NODE_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "nacl_io/error.h"
13#include "nacl_io/node.h"
14#include "nacl_io/pepper_interface.h"
15
16namespace nacl_io {
17
18typedef std::map<std::string, std::string> StringMap_t;
19
20class HttpFsNode : public Node {
21 public:
22  virtual Error FSync();
23  virtual Error GetDents(size_t offs,
24                         struct dirent* pdir,
25                         size_t count,
26                         int* out_bytes);
27  virtual Error GetStat(struct stat* stat);
28  virtual Error Read(const HandleAttr& attr,
29                     void* buf,
30                     size_t count,
31                     int* out_bytes);
32  virtual Error FTruncate(off_t size);
33  virtual Error Write(const HandleAttr& attr,
34                      const void* buf,
35                      size_t count,
36                      int* out_bytes);
37  virtual Error GetSize(off_t* out_size);
38
39  void SetCachedSize(off_t size);
40
41 protected:
42  HttpFsNode(Filesystem* filesystem,
43             const std::string& url,
44             bool cache_content);
45
46  virtual ~HttpFsNode();
47
48 private:
49  Error GetStat_Locked(struct stat* stat);
50  Error OpenUrl(const char* method,
51                StringMap_t* request_headers,
52                ScopedResource* out_loader,
53                ScopedResource* out_request,
54                ScopedResource* out_response,
55                int32_t* out_statuscode,
56                StringMap_t* out_response_headers);
57
58  Error DownloadToCache();
59  Error ReadPartialFromCache(const HandleAttr& attr,
60                             void* buf,
61                             int count,
62                             int* out_bytes);
63  Error DownloadPartial(const HandleAttr& attr,
64                        void* buf,
65                        off_t count,
66                        int* out_bytes);
67
68  Error DownloadToTemp(off_t* out_bytes);
69
70  // Read as much as possible from |loader|, using |buffer_| as a scratch area.
71  Error ReadEntireResponseToTemp(const ScopedResource& loader,
72                                 off_t* out_bytes);
73  // Read as much as possible from |loader|, storing the result in
74  // |cached_data_|.
75  Error ReadEntireResponseToCache(const ScopedResource& loader, int* out_bytes);
76
77  // Read up to |count| bytes from |loader|, using |buffer_| as a scratch area.
78  Error ReadResponseToTemp(const ScopedResource& loader,
79                           int count,
80                           int* out_bytes);
81
82  // Read up to |count| bytes from |loader|, and store the result in |buf|,
83  // which is assumed to have |count| bytes free.
84  Error ReadResponseToBuffer(const ScopedResource& loader,
85                             void* buf,
86                             int count,
87                             int* out_bytes);
88
89  std::string url_;
90  char* buffer_;
91  int buffer_len_;
92
93  bool cache_content_;
94  bool has_cached_size_;
95  std::vector<char> cached_data_;
96
97  friend class HttpFs;
98};
99
100}  // namespace nacl_io
101
102#endif  // LIBRARIES_NACL_IO_HTTPFS_HTTP_FS_NODE_H_
103