summaryrefslogtreecommitdiff
path: root/webtools.c
blob: b8537ee23ae6e51a846bc2afed7fa8850a30bce3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * webtools.c
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include "httpd.h"

//***************************************************************************
// Load from FS
//***************************************************************************

int cWebTools::loadFromFs(MemoryStruct* data, const char* filename, const char* subPath)
{
   char* path = 0;
   char* infile = 0;
   int status;

   asprintf(&path, "%s/%s", EpgdConfig.httpPath, subPath);
   chkDir(path);

   asprintf(&infile, "%s/%s", path, filename);
   status = loadFromFile(infile, data);
   free(infile);
   free(path);

   return status;
}

//***************************************************************************
// Get Modification Time Of File
//***************************************************************************

time_t cWebTools::getModTimeOf(const char* filename, const char* subPath)
{
   char* path = 0;
   char* file = 0;
   time_t t;

   asprintf(&path, "%s/%s", EpgdConfig.httpPath, subPath);
   chkDir(path);

   asprintf(&file, "%s/%s", path, filename);

   t = fileModTime(file);
   
   free(file);
   free(path);

   return t;
}

//***************************************************************************
// Get Integer Parameter
//***************************************************************************

int cWebTools::getIntParameter(MHD_Connection* tcp, const char* name, int def)
{
   const char* arg = MHD_lookup_connection_value(tcp, MHD_GET_ARGUMENT_KIND, name);

   return arg ? atoi(arg) : def;
}

//***************************************************************************
// Get String Parameter
//***************************************************************************

const char* cWebTools::getStrParameter(MHD_Connection* tcp, const char* name, const char* def)
{
   const char* arg = MHD_lookup_connection_value(tcp, MHD_GET_ARGUMENT_KIND, name);

   return arg ? arg : def;
}

//***************************************************************************
// Get Header Information
//***************************************************************************

const char* cWebTools::getStrHeader(MHD_Connection* tcp, const char* name, const char* def)
{
   const char* arg = MHD_lookup_connection_value(tcp, MHD_HEADER_KIND, name);

   return arg ? arg : def;
}
//***************************************************************************
// Get Time Header
//***************************************************************************

time_t cWebTools::getTimeHeader(MHD_Connection* tcp, const char* name, const char* def)
{
   time_t t = 0;
   struct tm tm;
   const char* str = getStrHeader(tcp, name, "");

   // Example: 'Mon, 31 Mar 2014 11:05:01 GMT'

   if (!isEmpty(str))
   {
      memset(&tm, 0, sizeof(tm));

      strptime(str, "%a, %d %b %Y %H:%M:%S", &tm);
      t = mktime(&tm);

      if (strstr(str, "GMT") || strstr(str, "UTC"))
         t += tmeSecondsPerHour;
   }
   
   return t;
}

//***************************************************************************
// Create HTTP Response
//***************************************************************************

MHD_Response* cWebTools::createHttpResponse(MemoryStruct* data)
{
   void* memory = data->isZipped() ? data->zmemory : data->memory;
   int size = data->isZipped() ? data->zsize : data->size;

#if MHD_VERSION >= 0x00090000
   return MHD_create_response_from_buffer(size, memory, MHD_RESPMEM_MUST_COPY); // MHD_RESPMEM_PERSISTENT)
#else
   return MHD_create_response_from_data(size, memory, no, yes);
#endif
}

//***************************************************************************
// Add HTTP Header Item
//***************************************************************************

int cWebTools::addHeaderItem(MHD_Response* response, const char* name, const char* value)
{
   return MHD_add_response_header(response, name, value);
}