Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
NetFileServlet.cpp
Go to the documentation of this file.
1 /* Project Starshatter 4.5
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: NetFileServlet.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  HTTP Servlet for File Transfer
13 */
14 
15 
16 #include "MemDebug.h"
17 #include "NetFileServlet.h"
18 #include "NetAdminServer.h"
19 #include "NetLayer.h"
20 
21 #include "DataLoader.h"
22 
23 // +-------------------------------------------------------------------+
24 
25 bool
27 {
28  if (!CheckUser(request, response))
29  return true;
30 
31  Text content;
32  Text path = request.GetParam("path");
33  Text name = request.GetParam("name");
34 
35  if (name.length()) {
36  BYTE* buffer = 0;
38 
39  if (loader) {
40  loader->SetDataPath(path);
41  int len = loader->LoadBuffer(name, buffer);
42 
43  if (len) {
44  content = Text((const char*) buffer, len);
45  }
46 
47  loader->ReleaseBuffer(buffer);
48  loader->SetDataPath(0);
49  }
50  }
51 
53  response.AddHeader("MIME-Version", "1.0");
54  response.AddHeader("Cache-Control", "no-cache");
55  response.AddHeader("Expires", "-1");
56  response.AddHeader("Content-Type", "text/plain");
57  response.SetContent(content);
58 
59  return true;
60 }
61 
62 // +-------------------------------------------------------------------+
63 
64 bool
66 {
67  Text content;
68  Text name = request.URI();
69  bool found = false;
70 
71  if (name.length() > 4) {
72  char filename[256];
73  strcpy_s(filename, name.data() + 1); // skip leading '/'
74 
75  FILE* f;
76  ::fopen_s(&f, filename, "rb");
77 
78  if (f) {
79  ::fseek(f, 0, SEEK_END);
80  int len = ftell(f);
81  ::fseek(f, 0, SEEK_SET);
82 
83  BYTE* buf = new(__FILE__,__LINE__) BYTE[len];
84 
85  ::fread(buf, len, 1, f);
86  ::fclose(f);
87 
88  content = Text((const char*) buf, len);
89  delete [] buf;
90 
91  found = true;
92  ::Print("weblog: 200 OK %s %d bytes\n", name.data(), len);
93  }
94  else {
95  ::Print("weblog: 404 Not Found %s\n", name.data());
96  }
97  }
98 
99  if (found) {
100  Text content_type = "text/plain";
101 
102  if (name.contains(".gif"))
103  content_type = "image/gif";
104  else if (name.contains(".jpg"))
105  content_type = "image/jpeg";
106  else if (name.contains(".htm"))
107  content_type = "text/html";
108 
109  response.SetStatus(HttpResponse::SC_OK);
110  response.AddHeader("MIME-Version", "1.0");
111  response.AddHeader("Content-Type", content_type);
112  response.SetContent(content);
113  }
114  else {
116  }
117 
118  return true;
119 }