ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

338 lines
6.9 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2015 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // See accompanying file COPYING.TXT file for licensing details.
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #ifndef CPPCMS_IMPL_HTTP_FILE_BUFFER_H
  9. #define CPPCMS_IMPL_HTTP_FILE_BUFFER_H
  10. #ifndef _FILE_OFFSET_BITS
  11. #define _FILE_OFFSET_BITS 64
  12. #endif
  13. #include <cppcms/config.h>
  14. #include <stdlib.h>
  15. #ifdef CPPCMS_HAVE_FSEEKO
  16. // nothing
  17. #elif defined(CPPCMS_HAVE_FSEEKI64)
  18. #define fseeko(f,o,w) _fseeki64(f,o,w)
  19. #else
  20. #define fseek(f,o,w) fseek(f,o,w)
  21. #endif
  22. #include <booster/nowide/cstdio.h>
  23. #include <cppcms/urandom.h>
  24. #include "tohex.h"
  25. #include <streambuf>
  26. #include <vector>
  27. #include <stdlib.h>
  28. namespace cppcms {
  29. namespace http {
  30. namespace impl {
  31. //
  32. // Properties:
  33. //
  34. // output - write only no seek
  35. // input - independent seek of write
  36. // if fime size is under certain limit - goes to memory
  37. // tracs full size via size() member function
  38. //
  39. class file_buffer : public std::streambuf {
  40. public:
  41. static const size_t buffer_size = 1024;
  42. file_buffer(size_t mlimit = 0) :
  43. in_memory_(true),
  44. f_(0),
  45. limit_(mlimit),
  46. file_size_(0),
  47. read_offset_(0)
  48. {
  49. setp(0,0);
  50. setg(0,0,0);
  51. }
  52. ~file_buffer()
  53. {
  54. if(f_)
  55. fclose(f_);
  56. }
  57. bool in_memory()
  58. {
  59. return in_memory_;
  60. }
  61. void set_limit(size_t mlimit)
  62. {
  63. if(in_memory_ && limit_ < mlimit) {
  64. limit_ = mlimit;
  65. }
  66. }
  67. void temp_dir(std::string const &tdir)
  68. {
  69. if(!in_memory_)
  70. throw booster::logic_error("Can't update temporary dir for open file");
  71. temp_dir_ = tdir;
  72. }
  73. std::string name()
  74. {
  75. return name_;
  76. }
  77. void name(std::string const &n)
  78. {
  79. if(!in_memory_)
  80. throw booster::logic_error("File name updated on open file");
  81. name_ = n;
  82. }
  83. int close()
  84. {
  85. if(sync() < 0)
  86. return -1;
  87. if(f_) {
  88. if(fclose(f_)!=0) {
  89. f_ = 0;
  90. return -1;
  91. }
  92. f_ = 0;
  93. }
  94. return 0;
  95. }
  96. #ifdef DEBUG_FILE_BUFFER
  97. void status(char const *name = "")
  98. {
  99. printf("------------ %s------------\n",name);
  100. printf(" in_memory=%d\n",in_memory_);
  101. printf(" limit = %d\n",int(limit_));
  102. printf(" pbuf = {%d|%d} of %d\n",int(pptr()-pbase()),int(epptr()-pptr()),int(epptr()-pbase()));
  103. printf(" gbuf = {%d|%d} of %d\n",int(gptr()-eback()),int(egptr()-gptr()),int(egptr()-eback()));
  104. printf(" file_size = %5lld\n",file_size_);
  105. printf(" read_offset = %5lld\n",read_offset_);
  106. printf("\n");
  107. }
  108. #endif
  109. long long size()
  110. {
  111. return file_size_ + (pptr() - pbase());
  112. }
  113. int to_file()
  114. {
  115. if(!in_memory_)
  116. return 0;
  117. size_t read_offset =gptr() - eback();
  118. if(write_buffer() != 0)
  119. return -1;
  120. clear(data_);
  121. output_.resize(buffer_size);
  122. setp(&output_[0],&output_[0]+buffer_size);
  123. setg(0,0,0);
  124. read_offset_ = read_offset;
  125. in_memory_=false;
  126. return 0;
  127. }
  128. private:
  129. void get_name()
  130. {
  131. if(!name_.empty())
  132. return;
  133. std::string tmp_dir;
  134. if(temp_dir_.empty()) {
  135. char const *tmp=getenv("TEMP");
  136. if(!tmp)
  137. tmp=getenv("TMP");
  138. if(!tmp)
  139. tmp="/tmp";
  140. tmp_dir=tmp;
  141. }
  142. else {
  143. tmp_dir = temp_dir_;
  144. }
  145. name_ = tmp_dir + "/cppcms_uploads_";
  146. urandom_device rnd;
  147. char buf[16];
  148. char rand[33]={0};
  149. rnd.generate(buf,16);
  150. cppcms::impl::tohex(buf,sizeof(buf),rand);
  151. name_.append(rand);
  152. name_+=".tmp";
  153. }
  154. protected:
  155. std::streampos seekpos(std::streampos off,std::ios_base::openmode mode)
  156. {
  157. return seekoff(off,std::ios_base::beg,mode);
  158. }
  159. std::streampos seekoff (std::streamoff off, std::ios_base::seekdir way,std::ios_base::openmode mode)
  160. {
  161. if(mode & std::ios_base::out) {
  162. if(off != 0 || way != std::ios_base::cur)
  163. return -1;
  164. return size();
  165. }
  166. if(in_memory_) {
  167. size_t rpos;
  168. size_t stream_size = pptr() - pbase();
  169. switch(way) {
  170. case std::ios_base::beg: rpos = off ; break;
  171. case std::ios_base::cur: rpos = gptr() - eback() + off; break;
  172. case std::ios_base::end: rpos = stream_size + off; break;
  173. default: return -1;
  174. }
  175. if(rpos > stream_size)
  176. return -1;
  177. setg(pbase(),pbase()+rpos,pptr());
  178. return rpos;
  179. }
  180. else {
  181. if(sync() < 0)
  182. return -1;
  183. read_offset_ += gptr() - eback();
  184. setg(0,0,0);
  185. long long new_offset;
  186. switch(way) {
  187. case std::ios_base::beg: new_offset = off; break;
  188. case std::ios_base::cur: new_offset = read_offset_ + off; break;
  189. case std::ios_base::end: new_offset = file_size_ + off; break;
  190. default: return -1;
  191. }
  192. if(new_offset < 0 || new_offset > file_size_) {
  193. new_offset = file_size_;
  194. return -1;
  195. }
  196. read_offset_ = new_offset;
  197. return read_offset_;
  198. }
  199. }
  200. int pbackfail(int)
  201. {
  202. if(in_memory_)
  203. return -1;
  204. if(read_offset_ == 0)
  205. return -1;
  206. int by = buffer_size / 2;
  207. if(read_offset_ < by)
  208. by = read_offset_;
  209. if(seekoff(-by,std::ios_base::cur,std::ios_base::in) < 0)
  210. return -1;
  211. if(underflow() < 0)
  212. return -1;
  213. gbump(by - 1);
  214. return std::char_traits<char>::to_int_type(*gptr());
  215. }
  216. int underflow()
  217. {
  218. if(in_memory_) {
  219. size_t read_size = gptr() - eback();
  220. setg(pbase(),pbase()+read_size,pptr());
  221. }
  222. else {
  223. if(sync() < 0)
  224. return -1;
  225. read_offset_ += gptr() - eback();
  226. if(fseeko(f_,read_offset_,SEEK_SET)!=0)
  227. return -1;
  228. input_.resize(buffer_size);
  229. char *input_data = &input_[0];
  230. size_t n = fread(input_data,1,buffer_size,f_);
  231. setg(input_data,input_data,input_data+n);
  232. }
  233. if(gptr()==egptr())
  234. return -1;
  235. return std::char_traits<char>::to_int_type(*gptr());
  236. }
  237. int overflow(int c)
  238. {
  239. size_t size = pptr() - pbase();
  240. if(in_memory_) {
  241. if(size >= limit_) {
  242. if(to_file() < 0)
  243. return -1;
  244. }
  245. else {
  246. size_t read_offset =gptr() - eback();
  247. size_t new_size = data_.size() * 2;
  248. if(new_size == 0)
  249. new_size = 64;
  250. if(new_size > limit_)
  251. new_size = limit_;
  252. data_.resize(new_size);
  253. setp(&data_[0],&data_[0] + data_.size());
  254. pbump(size);
  255. setg(pbase(),pbase() + read_offset,pptr());
  256. }
  257. }
  258. else {
  259. if(write_buffer()!= 0)
  260. return -1;
  261. setp(pbase(),epptr());
  262. }
  263. if(c!=EOF) {
  264. *pptr() = c;
  265. pbump(1);
  266. }
  267. return 0;
  268. }
  269. int sync()
  270. {
  271. if(in_memory_)
  272. return 0;
  273. if(write_buffer()!=0)
  274. return -1;
  275. if(fflush(f_)!=0)
  276. return -1;
  277. return 0;
  278. }
  279. int write_buffer()
  280. {
  281. if(!f_) {
  282. get_name();
  283. f_ = booster::nowide::fopen(name_.c_str(),"w+b");
  284. if(!f_)
  285. return -1;
  286. }
  287. if(fseek(f_,0,SEEK_END) !=0)
  288. return -1;
  289. size_t size = pptr()-pbase();
  290. if(size != 0 && fwrite(pbase(),1,size,f_)!=size)
  291. return -1;
  292. file_size_ += size;
  293. setp(pbase(),epptr());
  294. return 0;
  295. }
  296. private:
  297. void clear(std::vector<char> &v)
  298. {
  299. std::vector<char> tmp;
  300. tmp.swap(v);
  301. }
  302. bool in_memory_;
  303. FILE *f_;
  304. size_t limit_;
  305. long long file_size_;
  306. long long read_offset_;
  307. std::vector<char> input_;
  308. std::vector<char> output_;
  309. std::vector<char> data_;
  310. std::string temp_dir_;
  311. std::string name_;
  312. };
  313. } // impl
  314. } // http
  315. } // cppcms
  316. #endif