Commit graph

50 commits

Author SHA1 Message Date
WJQSERVER
f46db9659b
Merge pull request #28 from infinite-iroha/feat/memory-improvements-and-request-size-limit
Feat/memory improvements and request size limit
2025-06-21 22:46:32 +08:00
google-labs-jules[bot]
6339532d78 feat: Implement data binding, enhance HTML rendering, add context tests
This commit introduces several significant enhancements and completes key features:

1.  **Enhanced HTML Rendering:**
    - Defined a new `touka.HTMLRender` interface in `engine.go` for pluggable HTML rendering.
    - `Engine.HTMLRender` now uses this interface type.
    - `Context.HTML` is updated to use the configured `HTMLRender` instance, with improved error handling if no renderer is set or if rendering fails. The previous `fmt.Sprintf` fallback has been removed.
    - Added `DefaultHTMLRenderer` (using `html/template`) and helper methods `Engine.LoadHTMLGlob()` and `Engine.SetHTMLTemplate()` for easy setup.

2.  **Comprehensive Data Binding:**
    - **`Context.ShouldBindForm`**: Implemented using `gorilla/schema` to bind `x-www-form-urlencoded` and `multipart/form-data` (fields) from the request body.
    - **`Context.ShouldBindQuery`**: Implemented using `gorilla/schema` to bind URL query parameters.
    - **`Context.ShouldBindXML`**: Implemented using `encoding/xml` to bind XML data from the request body. This method also respects the `Engine.MaxRequestBodySize` limit.
    - **`Context.ShouldBind`**: Implemented as a generic binder that inspects the `Content-Type` header and dispatches to the appropriate specific binder (JSON, XML, Form). It handles missing or unsupported content types.

3.  **Comprehensive Unit Tests for `context.go`:**
    - Massively expanded `context_test.go` to provide extensive test coverage for nearly all methods in `context.go`.
    - This includes detailed tests for all new data binding methods, the updated HTML rendering logic, state management (`Keys`), request/response utilities, error handling, header and cookie manipulation, streaming, Go context integration, and logging.
    - Mocks for `HTMLRender`, `ErrorHandler`, and `reco.Logger` were used to facilitate thorough testing.

These changes significantly improve the framework's feature set, robustness, and maintainability due to increased test coverage.
2025-06-20 07:12:05 +00:00
google-labs-jules[bot]
82099e26ee feat: Add request body size limit and enhance error logging
This commit introduces two main improvements to the Touka web framework:

1.  **Configurable Request Body Size Limit:**
    - Added `MaxRequestBodySize int64` to `touka.Engine` (default 10MB).
    - You can customize this via `engine.SetMaxRequestBodySize()`.
    - The context methods `GetReqBodyFull()`, `GetReqBodyBuffer()`, and `ShouldBindJSON()` now adhere to this limit. They check `Content-Length` upfront and use `http.MaxBytesReader` to prevent reading excessively large request bodies into memory, enhancing protection against potential DoS attacks or high memory usage.
    - Added comprehensive unit tests in `context_test.go` for this feature, covering scenarios where the limit is active, disabled, and exceeded.

2.  **Enhanced Error Logging in Default Handler:**
    - The `defaultErrorHandle` in `engine.go` now logs not only the primary error passed to it but also any additional errors collected in `Context.Errors` (via `c.AddError()`).
    - This provides more comprehensive diagnostic information in the logs without altering the JSON error response structure sent to the client, ensuring backward compatibility.

These changes aim to improve the framework's robustness, memory safety, and debuggability.
2025-06-20 06:35:01 +00:00
wjqserver
543b3165ca optimize midwareX 2025-06-18 08:55:15 +08:00
wjqserver
f434f517d4 update middlewareX 2025-06-18 08:47:20 +08:00
wjqserver
00b9c283d1 update chain exp 2025-06-17 14:41:39 +08:00
wjqserver
21d048b5ab update UseIf 2025-06-17 14:34:52 +08:00
wjqserver
53544644af use new resolveRoutePath replace path.Join && add UseIf 2025-06-17 14:20:14 +08:00
wjqserver
bfc6b439e4 [context] add SetHeaders 2025-06-14 17:58:08 +08:00
wjqserver
ebb634b643 update default error handle 2025-06-13 17:53:48 +08:00
wjqserver
0076c7538c fix default error handle 2025-06-13 14:41:10 +08:00
wjqserver
57e92874d1 update deps 2025-06-12 10:02:33 +08:00
wjqserver
803a6747f6 update serve && add custom srv configure 2025-06-12 09:40:46 +08:00
wjqserver
896182417f fix header writer after status issue 2025-06-11 11:24:54 +08:00
wjqserver
9a2aeef0d0 remove ANY form MethodsSet to avoid conflict 2025-06-11 11:23:15 +08:00
wjqserver
bb822599b9 update methods 2025-06-11 11:11:54 +08:00
wjqserver
96154fff78 update deps 2025-06-10 21:52:32 +08:00
wjqserver
5ae9e9c12e updaten license 2025-06-10 21:39:37 +08:00
wjqserver
ce5efae287 update 2025-06-10 21:37:53 +08:00
wjqserver
e6b54eedbf remove tgzip 2025-06-10 00:04:15 +08:00
wjqserver
e891afe0b4 remove ws, move to github.com/fenthope/ws 2025-06-09 23:18:15 +08:00
wjqserver
90ea798c87 update README 2025-06-08 04:33:43 +08:00
wjqserver
a4c22962ad update go version 2025-06-07 23:45:02 +08:00
wjqserver
1d5ffac153 [context] optimize context reset and reuse 2025-06-06 22:40:40 +08:00
wjqserver
450d6294ad remove dup engine.pool.Put(c) 2025-06-06 22:13:17 +08:00
wjqserver
4249f0192e add IsHijacked for respw && add recover for flush 2025-06-06 21:44:45 +08:00
wjqserver
740dce54a2 optimize recovery 2025-06-06 21:43:49 +08:00
wjqserver
81fd3902cb fix ctx lifetime 2025-06-06 21:43:16 +08:00
wjqserver
0d6109f6da add adapter 2025-06-06 21:42:24 +08:00
wjqserver
61e67bc2a0 [context] fix writer reset 2025-06-06 21:29:09 +08:00
wjqserver
1618f89ba5 fix hijack 2025-06-06 01:25:35 +08:00
wjqserver
643fcd77ef update info 2025-06-05 21:06:09 +08:00
wjqserver
69469c0306 fix NoRoutes 2025-06-05 20:47:41 +08:00
wjqserver
c6c65c04a7 [engine] add NoRoutes 2025-06-05 20:31:18 +08:00
wjqserver
3f2dc21180 add testutil 2025-06-05 19:14:38 +08:00
wjqserver
757cd962ab [context] Add Cookie Method, port from gin 2025-06-05 18:35:28 +08:00
wjqserver
31d2065f07 [engine] add StaticFile 2025-06-04 21:50:03 +08:00
wjqserver
b8a1c5523a ctx add logreco method 2025-06-04 21:30:56 +08:00
wjqserver
899d3ff48b add logreco 2025-06-04 20:44:23 +08:00
wjqserver
385b6239c8 add GetHTTPC 2025-06-04 18:40:00 +08:00
wjqserver
6eac3b2046 add GetProtocol 2025-06-02 02:10:25 +08:00
wjqserver
dd4afc276b add auto websocket 2025-05-30 21:32:22 +08:00
wjqserver
d6aa97bbe5 add internal gzip 2025-05-30 21:32:06 +08:00
wjqserver
52b900db92 fix fileserver status chain 2025-05-30 21:31:54 +08:00
wjqserver
8ae88a77f0 add Raw & GetReqBodyFull 2025-05-30 16:51:38 +08:00
wjqserver
4f0363a62f fix mod 2025-05-29 21:48:22 +08:00
wjqserver
54ff216eff add static 2025-05-29 21:27:35 +08:00
wjqserver
5a10829041 add ErrorUseHandle 2025-05-28 21:34:08 +08:00
wjqserver
484f2f016b init(v0.0.1) 2025-05-28 18:25:28 +08:00
WJQSERVER
aea609ce29
Initial commit 2025-05-28 18:12:23 +08:00