summaryrefslogtreecommitdiffstats
path: root/Designs/webserver_design.md
blob: c6b61ef21c8f6d39f459966962b0d7f9c8126a76 (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
This is the API design of the webserver supporting the browser source.

I tried oatpp but it had a nasty habit of randomly crashing. I looked at
crowCPP but it depends on boost, which I have a personal grudge against.
So I'm making Yet Another Web Framework.

This one supports only the minimum feature set required to implement the
browser source.

The browser source requires two APIs:
1. GET /: return index.html (html)
2. GET /api/transcript: return transcript (json)

```
enum ContentType {
  HTTP,
  JSON,
};

class WebServer:
public:
  WebServer(u16 port)

  // method and path are in-params
  // payload and type are out-params
  typedef handler_t std::function<void(const string method, const string path,
      string& payload, ContentType& type);

  void RegisterPathHandler(string method, string path, handler_t handler);

  Run(volatile bool* run)
```

We need a way to generate HTML from responses:
```
class HTTPMapper:
  HTTPMapper()

  void Map(const string method, const string path, const string payload, const
      ContentType type) = 0;

class HTTPMapperHTML : public HTTPMapper:
  HTTPMapperHTML()

  ...

class HTTPMapperJSON : public HTTPMapper:
  HTTPMapperJSON()

  ...
```

We also need a way to parse client-sent HTML:
```
class HTTPParser:
  HTTPParser()

  bool Parse(const string payload)

  string GetMethod();
  string GetPath();
  bool GetHeader(const string header, string value)

  bool GetPayload()
```

WebServer hides HTTPMapper.
WebServer and HTTPMapper share ContentType.

So we have

  WebCommon.h: shared types
  HTTPParser.h: supporting API for WebServer
  HTTPMapper.h: supporting API for WebServer
  WebServer.h: user-visible API