|
| 1 | +# Refactoring |
| 2 | + |
| 3 | +This document describes the refactoring and migration process from version `v0.1` to `v0.2` and later to `v0.3`. |
| 4 | + |
| 5 | +> [!NOTE] |
| 6 | +> This document is a work in progress. |
| 7 | +
|
| 8 | +## Version 0.2 |
| 9 | + |
| 10 | +In version `v0.1`, the HTTP parser library is not cleanly structured. Memory handling is mixed with parser functionality, which makes the library difficult to reuse in external projects. |
| 11 | + |
| 12 | +The goal of the refactoring is to make the library usable both for the internal Arduino microcontroller port and for other external projects. |
| 13 | + |
| 14 | +Version `v0.2` will: |
| 15 | + |
| 16 | +- Remove memory handling from the library completely |
| 17 | +- Provide stable, secure, and browser-compatible processing for HTTP requests (initially only GET and POST) |
| 18 | +- Add and update library documentation |
| 19 | + |
| 20 | +> [!NOTE] |
| 21 | +> Version `v0.2` is intended to provide a tested and reliable library for use in external projects. |
| 22 | +
|
| 23 | +### 1. HTTP-Parser |
| 24 | + |
| 25 | +1. The HTTP parser should only handle HTTP/1.1 requests |
| 26 | +2. Remove C++ exceptions and replace them with direct, explicit object status checks (`if` clauses) |
| 27 | +3. Remove or decapsulate shared memory management functionality |
| 28 | +4. Improve GET parameter parsing and result handling |
| 29 | +5. Add limit checks to guarantee fail-safe operation |
| 30 | +6. Add a request queueing and request-handling mechanism |
| 31 | +7. Add tests and improve code quality |
| 32 | +8. Ensure C++11 conformity (for embedded compatibility, smaller binaries, and security) |
| 33 | + |
| 34 | +#### 1.1. HTTP/1.1 Requests Only |
| 35 | + |
| 36 | +The parser will be restricted to HTTP/1.1 request handling only. This simplifies the implementation, reduces ambiguity, and avoids the need to support incompatible or unnecessary protocol variants for the embedded target. |
| 37 | + |
| 38 | +#### 1.2. Remove C++ Exceptions |
| 39 | + |
| 40 | +C++ exceptions will be removed from the parser library and replaced with explicit status reporting and conditional checks. This improves portability, makes control flow easier to follow, and is better suited for embedded environments. |
| 41 | + |
| 42 | +#### 1.3. Memory Management |
| 43 | + |
| 44 | +Memory management responsibilities will be removed from the parser or clearly separated from its core functionality. The parser should focus only on protocol parsing and validation so that it can be reused in other projects without depending on a specific memory model. |
| 45 | + |
| 46 | +#### 1.4. GET Parameter Parsing |
| 47 | + |
| 48 | +GET parameter parsing will be improved to produce clearer and more reliable results. This includes better separation of keys and values, more predictable result structures, and safer handling of malformed or incomplete query strings. |
| 49 | + |
| 50 | +#### 1.5. Limits Checking |
| 51 | + |
| 52 | +Additional limit checks will be introduced to guarantee fail-safe behavior. This includes checking request sizes, buffer boundaries, header counts, and other parser limits to prevent invalid input from causing unsafe behavior. |
| 53 | + |
| 54 | +#### 1.6. Result Queuing |
| 55 | + |
| 56 | +A request/result queueing mechanism will be added to improve how parsed requests are handed off for further processing. This is intended to make request handling more robust and to prepare the parser for use in threaded or constrained runtime environments. |
| 57 | + |
| 58 | +#### 1.7. Tests / Code Quality |
| 59 | + |
| 60 | +Test coverage will be expanded and the general code quality will be improved. This includes adding regression tests, validating parser edge cases, and restructuring code where necessary to make behavior easier to verify and maintain. |
| 61 | + |
| 62 | +#### 1.8. C++11 Conformity |
| 63 | + |
| 64 | +The refactored parser will be kept compatible with C++11. This is important for embedded toolchains and helps keep the codebase portable, efficient, and easier to integrate into constrained environments such as Arduino-based targets. |
| 65 | + |
| 66 | +### 2. HTTP Response Generator |
| 67 | + |
| 68 | +In addition to the HTTP parser, the library also contains an HTTP response generator. Its purpose is to build valid and consistent HTTP/1.1 responses in a structured way, so that application code does not need to assemble response messages manually. |
| 69 | + |
| 70 | +The response generator should make it easier to produce standards-compliant responses, reduce duplicated formatting logic, and improve maintainability for both embedded and external use cases. The following sub-topics describe the planned responsibilities and behavior of the response generator in more detail. |
| 71 | + |
| 72 | +#### 2.1. Result Code |
| 73 | + |
| 74 | +The response generator will provide a clear and reliable way to set the HTTP result code for a response, such as `200`, `404`, or `500`. It should ensure that valid status codes are used consistently and that application code can assign them without manually constructing the status line each time. |
| 75 | + |
| 76 | +#### 2.2. Result Message |
| 77 | + |
| 78 | +In addition to the numeric result code, the response generator will handle the associated result message, such as `OK`, `Not Found`, or `Internal Server Error`. This should allow the library to generate complete and readable HTTP status lines while keeping the mapping between result codes and messages predictable and maintainable. |
| 79 | + |
| 80 | +#### 2.3. Date Header |
| 81 | + |
| 82 | +The response generator will automatically support creation of the `Date` header for HTTP responses. This is important for standards compliance and interoperability with browsers and other clients. The implementation should ensure that the date is generated in the correct HTTP format and can be included consistently in every response where required. |
| 83 | + |
| 84 | +#### 2.4. Adding Custom Headers |
| 85 | + |
| 86 | +The response generator will provide a mechanism for adding custom headers to a response in a controlled and extensible way. This includes headers such as `Content-Type`, `Cache-Control`, or application-specific metadata. The design should make it possible to add, update, and serialize headers cleanly without mixing header construction logic directly into application code. |
| 87 | + |
| 88 | +### Version 0.3 |
| 89 | + |
| 90 | +Version `v0.3` focuses on simplifying the current runtime architecture in `src`. |
| 91 | +At the moment, request parsing, application-server execution, result handling, and socket output are distributed across multiple components and processes. This increases complexity in coordination, shared-memory usage, descriptor passing, and object ownership. |
| 92 | + |
| 93 | +Version `v0.3` will: |
| 94 | + |
| 95 | +1. Remove the dedicated result-processing pipeline |
| 96 | +2. Move result handling back into the main server process |
| 97 | +3. Eliminate client file-descriptor passing between processes or threads |
| 98 | +4. Reduce shared-memory usage to application-server communication only |
| 99 | +5. Clarify the object roles of `ClientHandler`, `Client`, and `HTTPParser` |
| 100 | +6. Add an XML protocol parser library with queueing support |
| 101 | +7. Replace result ordering with request UUID handling |
| 102 | +8. Add a client request library including tests |
| 103 | +9. Integrate parsing and encryption in a fixed-size threaded model with protected request and result queues |
| 104 | +10. Implement shared memory data exchange between AS processes via SHMVector.cpp (currently named CustomVector.hpp) |
| 105 | + |
| 106 | +> [!NOTE] |
| 107 | +> The planned `v0.3` work is based on the current source layout in `/src`, especially `Server`, `ClientHandler`, `ASRequestHandler`, `ASProcessHandler`, `ResultProcessor`, `ResultOrder`, and `ThreadHandler`. |
| 108 | +
|
| 109 | +#### 1.1. Remove Dedicated Result Processing |
| 110 | + |
| 111 | +The current implementation uses `ResultProcessor`, `ResultOrder`, and `ThreadHandler` as separate stages for response processing. Version `v0.3` will remove this pipeline and simplify response generation and delivery. |
| 112 | + |
| 113 | +#### 1.2. Move Result Handling into the Main Server Process |
| 114 | + |
| 115 | +Result processing should no longer run in a separate process model. Instead, the main server flow should remain responsible for coordinating request completion and emitting final responses. |
| 116 | + |
| 117 | +#### 1.3. Remove File-Descriptor Passing |
| 118 | + |
| 119 | +The current architecture requires passing `clientFD` values between processes. Version `v0.3` should eliminate this mechanism so that the component writing the response already owns the client connection. |
| 120 | + |
| 121 | +#### 1.4. Reduce Shared-Memory Usage |
| 122 | + |
| 123 | +Shared memory should no longer be used for the full request/response lifecycle. It should be limited to backend or application-server communication where cross-process exchange is still necessary. |
| 124 | + |
| 125 | +#### 1.5. Clarify Client and Parser Responsibilities |
| 126 | + |
| 127 | +The current relationships between `ClientHandler`, `Client`, and `HTTPParser` are mixed. Version `v0.3` will establish clearer ownership between connection state, request state, and parsing logic. |
| 128 | + |
| 129 | +#### 1.6. Add an XML Protocol Parser Library |
| 130 | + |
| 131 | +An additional XML protocol parser library will be introduced following the same principles as the refactored HTTP parser. It should provide structured results, queueing support, and efficient transfer using move semantics. |
| 132 | + |
| 133 | +#### 1.7. Replace Result Ordering with Request UUIDs |
| 134 | + |
| 135 | +The current result model depends on explicit ordering logic such as `ReqNr` and `_LastRequest`. Version `v0.3` will replace this approach with request UUID handling. |
| 136 | + |
| 137 | +#### 1.8. Add a Client Request Library |
| 138 | + |
| 139 | +In addition to the protocol parsing library, a client request library with tests will be added for generating and sending requests in external projects. |
| 140 | + |
| 141 | +#### 1.9. Add a Fixed-Size Threaded Processing Model |
| 142 | + |
| 143 | +Version `v0.3` will introduce a controlled threaded model with a fixed number of worker threads, explicit request and result queues, atomic synchronization, and integrated parsing and encryption stages. |
| 144 | + |
| 145 | +#### 1.10. Improve Shared Memory Data Exchange |
| 146 | + |
| 147 | +A well-tested header-only `src/CustomVector.hpp` utility has been introduced to exchange data directly with an in-SHM placed C++ object instance. The container supports passing structs and uses atomic / spinlock-based synchronization for thread-safe / process-safe access, including `getNextElement()` for producer / consumer-style access patterns. |
| 148 | + |
| 149 | +## Future |
| 150 | + |
| 151 | +The following items are planned for later stages after the core `v0.3` refactoring is completed: |
| 152 | + |
| 153 | +- Adapt static file transmission into **new** NLAFP component |
| 154 | +- Add NETCONF integration |
0 commit comments