8000 [pull] main from miniflux:main by pull[bot] · Pull Request #348 · suhaoGe/miniflux · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
160 changes: 160 additions & 0 deletions internal/api/api_integration_test.go

Large diffs are not rendered by default.

59 changes: 37 additions & 22 deletions internal/cli/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ func startDaemon(store *storage.Storage) {
signal.Notify(stop, os.Interrupt)
signal.Notify(stop, syscall.SIGTERM)

reload := make(chan os.Signal, 1)
signal.Notify(reload, syscall.SIGHUP)

pool := worker.NewPool(store, config.Opts.WorkerPoolSize())

if config.Opts.HasSchedulerService() && !config.Opts.HasMaintenanceMode() {
runScheduler(store, pool)
}

var httpServers []*http.Server
var certReloadFn func()
if config.Opts.HasHTTPService() {
httpServers = server.StartWebServer(store, pool)
httpServers, certReloadFn = server.StartWebServer(store, pool)
}

metricsCtx, cancelMetrics := context.WithCancel(context.Background())
Expand Down Expand Up @@ -74,29 +78,40 @@ func startDaemon(store *storage.Storage) {
}
}

<-stop
slog.Debug("Shutting down the process")
cancelMetrics()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

if len(httpServers) > 0 {
slog.Debug("Shutting down HTTP servers...")
for _, server := range httpServers {
if server != nil {
if err := server.Shutdown(ctx); err != nil {
slog.Error("HTTP server shutdown error", slog.Any("error", err), slog.String("addr", server.Addr))
for {
select {
case <-stop:
slog.Debug("Shutting down the process")
cancelMetrics()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

if len(httpServers) > 0 {
slog.Debug("Shutting down HTTP servers...")
for _, srv := range httpServers {
if srv != nil {
if err := srv.Shutdown(ctx); err != nil {
slog.Error("HTTP server shutdown error", slog.Any("error", err), slog.String("addr", srv.Addr))
}
}
}
slog.Debug("All H 8000 TTP servers shut down.")
} else {
slog.Debug("No HTTP servers to shut down.")
}
}
slog.Debug("All HTTP servers shut down.")
} else {
slog.Debug("No HTTP servers to shut down.")
}

slog.Debug("Shutting down worker pool...")
pool.Shutdown()
slog.Debug("Worker pool shut down.")
slog.Debug("Shutting down worker pool...")
pool.Shutdown()
slog.Debug("Worker pool shut down.")

slog.Debug("Process gracefully stopped")
slog.Debug("Process gracefully stopped")
return

case <-reload:
slog.Info("Received SIGHUP, reloading TLS certificates")
if certReloadFn != nil {
certReloadFn()
}
}
}
}
97 changes: 97 additions & 0 deletions internal/http/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@
package client // import "miniflux.app/v2/internal/http/client"

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"time"

"miniflux.app/v2/internal/config"
"miniflux.app/v2/internal/urllib"
"miniflux.app/v2/internal/version"
)

const defaultRequestTimeout = 10 * time.Second

// ErrPrivateNetwork is returned when a connection to a private network is blocked.
var ErrPrivateNetwork = errors.New("client: connection to private network is blocked")

Expand Down Expand Up @@ -68,3 +75,93 @@ func NewClientWithOptions(opts Options) *http.Client {
Transport: transport,
}
}

// requestBuilder builds and executes HTTP requests with the builder pattern.
type requestBuilder struct {
err error
endpoint string
method string
body io.Reader
headers http.Header
}

// NewRequestBuilder creates a new request builder for the given endpoint.
func NewRequestBuilder(endpoint string) *requestBuilder {
return &requestBuilder{
endpoint: endpoint,
method: http.MethodGet,
headers: make(http.Header),
}
}

// WithMethod sets the HTTP method.
func (r *requestBuilder) WithMethod(method string) *requestBuilder {
r.method = method
return r
}

// WithHeader sets a header value.
func (r *requestBuilder) WithHeader(key, value string) *requestBuilder {
r.headers.Set(key, value)
return r
}

// WithJSON marshals payload as JSON, sets the body and Content-Type.
func (r *requestBuilder) WithJSON(payload any) *requestBuilder {
requestBody, err := json.Marshal(payload)
if err != nil {
r.err = fmt.Errorf("unable to encode request body: %w", err)
return r
}

return r.WithJSONBody(requestBody)
}

// WithJSONBody sets an already-marshaled JSON body and the Content-Type.
// It is useful when the caller needs the encoded payload for another
// purpose (e.g. computing a signature) to avoid marshaling it twice.
func (r *requestBuilder) WithJSONBody(body []byte) *requestBuilder {
r.body = bytes.NewReader(body)
r.headers.Set("Content-Type", "application/json")
return r
}

// Do builds and executes the request.
//
// Private networks are blocked unless explicitly allowed through the
// INTEGRATION_ALLOW_PRIVATE_NETWORKS option.
func (r *requestBuilder) Do() (*http.Response, error) {
if r.err != nil {
return nil, r.err
}

// The request is assembled lazily here rather than being stored as a
// prebuilt *http.Request in the builder: http.NewRequest inspects the
// body's concrete type (e.g. *bytes.Reader) to populate ContentLength and
// GetBody. Constructing it only once the body is known yields a correct
// Content-Length header and lets the client replay the body on redirects.
req, err := http.NewRequest(r.method, r.endpoint, r.body)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}

for key, values := range r.headers {
for _, value := range values {
req.Header.Add(key, value)
}
}

req.Header.Set("User-Agent", "Miniflux/"+version.Version)

clientOptions := Options{
Timeout: defaultRequestTimeout,
BlockPrivateNetworks: !config.Opts.IntegrationAllowPrivateNetworks(),
}

response, err := NewClientWithOptions(clientOptions).Do(req)
if err != nil {
return nil, fmt.Errorf("unable to send request: %w", err)
}

return response, nil
}
10000
74 changes: 74 additions & 0 deletions internal/http/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ package client

import (
"errors"
"io"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"

"miniflux.app/v2/internal/config"
"miniflux.app/v2/internal/version"
)

func TestNewClientWithoutBlockingPrivateNetworks(t *testing.T) {
Expand Down Expand Up @@ -111,3 +115,73 @@ func TestBlockPrivateNetworksAllowsLoopbackWhenDisabled(t *testing.T) {
t.Fatalf("Expected status 200, got %d", resp.StatusCode)
}
}

func TestRequestBuilderWithJSON(t *testing.T) {
configureIntegrationAllowPrivateNetworksOption(t)

var gotMethod, gotContentType, gotUserAgent, gotAuth, gotBody string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotMethod = r.Method
gotContentType = r.Header.Get("Content-Type")
gotUserAgent = r.Header.Get("User-Agent")
gotAuth = r.Header.Get("Authorization")
body, _ := io.ReadAll(r.Body)
gotBody = string(body)
w.WriteHeader(http.StatusCreated)
}))
defer server.Close()

response, err := NewRequestBuilder(server.URL).
WithMethod(http.MethodPost).
WithHeader("Authorization", "Bearer secret").
WithJSON(map[string]string{"hello": "world"}).
Do()
if err != nil {
t.Fatalf("request execution failed: %v", err)
}
defer response.Body.Close()

if response.StatusCode != http.StatusCreated {
t.Errorf("expected status %d, got %d", http.StatusCreated, response.StatusCode)
}
if gotMethod != http.MethodPost {
t.Errorf("expected method POST, got %s", gotMethod)
}
if gotContentType != "application/json" {
t.Errorf("expected Content-Type application/json, got %q", gotContentType)
}
if want := "Miniflux/" + version.Version; gotUserAgent != want {
t.Errorf("expected User-Agent %q, got %q", want, gotUserAgent)
}
if gotAuth != "Bearer secret" {
t.Errorf("expected Authorization %q, got %q", "Bearer secret", gotAuth)
}
if gotBody != `{"hello":"world"}` {
t.Errorf("expected body %q, got %q", `{"hello":"world"}`, gotBody)
}
}

func TestRequestBuilderWithInvalidEndpoint(t *testing.T) {
_, err := NewRequestBuilder("://invalid").WithMethod(http.MethodPost).WithJSON(nil).Do()
if err == nil {
t.Fatal("expected an error for an invalid endpoint, got nil")
}
}

func configureIntegrationAllowPrivateNetworksOption(t *testing.T) {
t.Helper()

t.Setenv("INTEGRATION_ALLOW_PRIVATE_NETWORKS", "1")

configParser := config.NewConfigParser()
parsedOptions, err := configParser.ParseEnvironmentVariables()
if err != nil {
t.Fatalf("Unable to configure test options: %v", err)
}

previousOptions := config.Opts
config.Opts = parsedOptions
t.Cleanup(func() {
config.Opts = previousOptions
})
}
Loading
0