Skip to content

Commit

Permalink
Fix branch file cannot be downloaded Modify listening port
Browse files Browse the repository at this point in the history
  • Loading branch information
iuu6 authored Feb 23, 2024
1 parent a88052e commit 2127b9a
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"regexp"
"strings"
Expand All @@ -27,7 +28,7 @@ var (
func main() {
http.HandleFunc("/", handler)
http.HandleFunc("/favicon.ico", iconHandler)
http.ListenAndServe(":8080", nil)
http.ListenAndServe(":5340", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -44,6 +45,13 @@ func handler(w http.ResponseWriter, r *http.Request) {

fmt.Println("Received URL:", u)

// 解码 URL
u, err := url.PathUnescape(u)
if err != nil {
http.Error(w, "Failed to decode URL.", http.StatusInternalServerError)
return
}

if m := checkURL(u); m != nil {
// For demonstration, just printing the matched groups
fmt.Printf("Author: %s, Repo: %s\n", m["author"], m["repo"])
Expand All @@ -57,6 +65,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
}
}


func index(w http.ResponseWriter, r *http.Request) {
// 提供 index.html 文件内容
http.ServeFile(w, r, "index.html")
Expand Down Expand Up @@ -103,21 +112,24 @@ func proxy(w http.ResponseWriter, r *http.Request) {
if matches := regexp.MustCompile(`filename="?([^"]+)"?`).FindStringSubmatch(disposition); len(matches) > 1 {
filename = matches[1]
}
} else {
// 从 URL 中提取文件名
parts := strings.Split(u, "/")
filename = parts[len(parts)-1]
}

w.Header().Set("Content-Disposition", "attachment; filename="+filename)
w.Header().Set("Content-Type", resp.Header.Get("Content-Type"))
w.Header().Set("Content-Length", resp.Header.Get("Content-Length"))

ch := iterContent(resp, 1024)
for chunk := range ch {
if _, err := w.Write(chunk); err != nil {
fmt.Println("Error writing response:", err)
return
}
// 直接传输响应体
if _, err := io.Copy(w, resp.Body); err != nil {
fmt.Println("Error writing response:", err)
return
}
}



func iterContent(r *http.Response, chunkSize int) <-chan []byte {
ch := make(chan []byte)

Expand Down

0 comments on commit 2127b9a

Please sign in to comment.