Skip to content

Commit

Permalink
fix: POST too large
Browse files Browse the repository at this point in the history
  • Loading branch information
j1g5awi committed Apr 11, 2022
1 parent 78a40d5 commit 15c2c8e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
- [x] Kindle
- 稍后读加载本地文件

由于每次自动同步时都直接读取本地文件然后返回,文件大了可能会有性能问题。

稍后读设置里请不要勾选「使用简悦 · 同步助手内置的解析器」,此功能无法实现。

## Usage
Expand All @@ -35,7 +33,7 @@ config.json 默认配置如下:
"smtpPort": 465,
"smtpUsername": "",
"smtpPassword": "",
"mailTitle": "[简悦] - {{ title }}",
"mailTitle": "[简悦] - {{title}}",
"receiverMail": "",
"kindleMail": ""
}
Expand Down
32 changes: 20 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,28 +186,35 @@ func verifyHandle(w http.ResponseWriter, r *http.Request) {
log.Println("verify success")
}

// 规避标准库大小限制
func myParseForm(r *http.Request) error {
b, err := io.ReadAll(r.Body)
if err != nil {
return err
}
vs, err := url.ParseQuery(string(b))
if err != nil {
return err
}
r.Form = make(url.Values)
for k, vs := range vs {
r.Form[k] = append(r.Form[k], vs...)
}
return nil
}

var etag string

// 如果浏览器插件的设置项更改了,它会发一个 key 为 config 的请求,json 返回 200
// 剩余情况下,返回一个 key 为 result 的 json
func configHandle(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
if syncPath != "" {
// 规避标准库大小限制
b, err := io.ReadAll(r.Body)
err := myParseForm(r)
if err != nil {
log.Println(err)
return
}
vs, err := url.ParseQuery(string(b))
if err != nil {
log.Println(err)
return
}
r.Form = make(url.Values)
for k, vs := range vs {
r.Form[k] = append(r.Form[k], vs...)
}

if data := r.Form.Get("config"); data != "" {
err := ioutil.WriteFile(filepath.Join(syncPath, "simpread_config.json"), []byte(data), 0644)
Expand Down Expand Up @@ -287,11 +294,12 @@ func configHandle(w http.ResponseWriter, r *http.Request) {
func plainHandle(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")

err := r.ParseForm()
err := myParseForm(r)
if err != nil {
log.Println(err)
return
}

title := r.Form.Get("title")
content := r.Form.Get("content")

Expand Down

0 comments on commit 15c2c8e

Please sign in to comment.