Skip to main content

http-https

HTTP/HTTPS protocol implementation and best practices

Source facts

Repository
ffsshhttiikk/opencode-agents-skills
Last source activity
February 28, 2026 at 22:51
Detected SKILL.md language
English
Stars
3
Forks
2

Install options

The review-first prompt is selected by default. You can switch to a direct command or download a local copy.

Review the source files

Read SKILL.md and any companion files shown by SkillsMP before deciding whether to install.

Showing SKILL.md

SKILL.md
Source instructions · Read-only preview
name
http-https
description
HTTP/HTTPS protocol implementation and best practices
category
networking
difficulty
intermediate
tags
["http","https","protocol","web","rest"]
author
OpenCode Community
version
1
last_updated
2024-01-15T00:00:00.000Z
# HTTP/HTTPS Protocols ## What I Do I am HTTP/HTTPS, the application layer protocol enabling communication between clients and servers on the World Wide Web. I define the rules for requesting and transferring web resources through methods (GET, POST, PUT, DELETE, etc.), status codes, headers, and message bodies. HTTPS adds Transport Layer Security (TLS) encryption to protect data in transit. I support persistent connections, chunked transfer encoding, content negotiation, and caching mechanisms. HTTP/2 and HTTP/3 introduce multiplexing, header compression, and server push for improved performance. I form the foundation for RESTful APIs, web services, and modern web applications. ## When to Use Me - Building RESTful APIs and web services - Client-server communication in web applications - Implementing microservices architecture - Webhook and callback integrations - File upload and download services - Real-time communication (WebSocket, Server-Sent Events) - API authentication and authorization - Content delivery and caching strategies ## Core Concepts **HTTP Methods**: GET (retrieve), POST (create), PUT (update), PATCH (partial update), DELETE (remove). **Status Codes**: 1xx (informational), 2xx (success), 3xx (redirection), 4xx (client error), 5xx (server error). **Headers**: Metadata controlling caching, authentication, content type, and request behavior. **Body/Payload**: Request or response content in various formats (JSON, XML, multipart, streaming). **Cookies and Sessions**: State management across HTTP requests. **Caching**: Headers (Cache-Control, ETag, Last-Modified) controlling response caching. **Content Negotiation**: Headers (Accept, Accept-Language) for format and language preferences. **HTTPS/TLS**: Encryption, certificate validation, and secure communication. ## Code Examples ### Example 1: HTTP Client with Retry and Circuit Breaker (Go) ```go package main import ( "context" "encoding/json" "fmt" "io" "log" "math" "net/http" "sync" "time" ) type HTTPClient struct { client *http.Client baseURL string defaultTimeout time.Duration retryConfig RetryConfig circuitBreaker *CircuitBreaker } type RetryConfig struct { MaxRetries int InitialDelay time.Duration MaxDelay time.Duration Multiplier float64 } type CircuitBreaker struct { name string failureCount int failureThreshold int resetTimeout time.Duration lastFailure time.Time state string // closed, open, half-open mutex sync.RWMutex } func NewHTTPClient(baseURL string, timeout time.Duration) *HTTPClient { return &HTTPClient{ client: &http.Client{ Timeout: timeout, Transport: &http.Transport{ MaxIdleConns: 100, MaxIdleConnsPerHost: 10, IdleConnTimeout: 90 * time.Second, MaxConnsPerHost: 100, }, }, baseURL: baseURL, defaultTimeout: timeout, retryConfig: RetryConfig{ MaxRetries: 3, InitialDelay: 100 * time.Millisecond, MaxDelay: 10 * time.Second, Multiplier: 2.0, }, circuitBreaker: &CircuitBreaker{ name: "http-circuit-breaker", failureThreshold: 5, resetTimeout: 30 * time.Second, state: "closed", }, } } func (c *HTTPClient) Get(ctx context.Context, endpoint string, headers map[string]string) (*HTTPResponse, error) { return c.doRequest(ctx, http.MethodGet, endpoint, nil, headers) } func (c *HTTPClient) Post(ctx context.Context, endpoint string, body interface{}, headers map[string]string) (*HTTPResponse, error) { return c.doRequest(ctx, http.MethodPost, endpoint, body, headers) } func (c *HTTPClient) Put(ctx context.Context, endpoint string, body interface{}, headers map[string]string) (*HTTPResponse, error) { return c.doRequest(ctx, http.MethodPut, endpoint, body, headers) } func (c *HTTPClient) Delete(ctx context.Context, endpoint string, headers map[string]string) (*HTTPResponse, error) { return c.doRequest(ctx, http.MethodDelete, endpoint, nil, headers) } func (c *HTTPClient) doRequest(ctx context.Context, method, endpoint string, body interface{}, headers map[string]string) (*HTTPResponse, error) { if !c.circuitBreaker.canExecute() { return nil, fmt.Errorf("circuit breaker is open") } var bodyReader io.Reader if body != nil { jsonBody, err := json.Marshal(body) if err != nil { return nil, fmt.Errorf("failed to marshal request body: %w", err) } bodyReader = bytes.NewReader(jsonBody) if headers == nil { headers = make(map[string]string) } headers["Content-Type"] = "application/json" } url := c.baseURL + endpoint req, err := http.NewRequestWithContext(ctx, method, url, bodyReader) if err != nil { return nil, fmt.Errorf("failed to create request: %w", err) } for key, value := range headers { req.Header.Set(key, value) } var lastErr error var response *http.Response delay := c.retryConfig.InitialDelay for attempt := 0; attempt <= c.retryConfig.MaxRetries; attempt++ { response, lastErr = c.client.Do(req) if lastErr == nil { break } if attempt == c.retryConfig.MaxRetries { c.circuitBreaker.recordFailure() return nil, fmt.Errorf("request failed after %d attempts: %w", c.retryConfig.MaxRetries, lastErr) } select { case <-ctx.Done(): return nil, ctx.Err() case <-time.After(delay): } delay = time.Duration(float64(delay) * c.retryConfig.Multiplier) delay = time.Duration(math.Min(float64(delay), float64(c.retryConfig.MaxDelay))) } defer response.Body.Close() responseBody, err := io.ReadAll(response.Body) if err != nil { return nil, fmt.Errorf("failed to read response body: %w", err) } httpResponse := &HTTPResponse{ StatusCode: response.StatusCode, Headers: response.Header, Body: responseBody, } if response.StatusCode >= 400 { c.circuitBreaker.recordFailure() return httpResponse, fmt.Errorf("HTTP error %d: %s", response.StatusCode, string(responseBody)) } c.circuitBreaker.recordSuccess() return httpResponse, nil } func (cb *CircuitBreaker) canExecute() bool { cb.mutex.RLock() defer cb.mutex.RUnlock() if cb.state == "open" { if time.Since(cb.lastFailure) > cb.resetTimeout { cb.mutex.RUnlock() cb.mutex.Lock() cb.state = "half-open" cb.mutex.Unlock() cb.mutex.RLock() } } return cb.state != "open" } func (cb *CircuitBreaker) recordFailure() { cb.mutex.Lock() defer cb.mutex.Unlock() cb.failureCount++ cb.lastFailure = time.Now() if cb.failureCount >= cb.failureThreshold { cb.state = "open" } } func (cb *CircuitBreaker) recordSuccess() { cb.mutex.Lock() defer cb.mutex.Unlock() cb.failureCount = 0 cb.state = "closed" } type HTTPResponse struct { StatusCode int Headers http.Header Body []byte } func (r *HTTPResponse) UnmarshalJSON(v interface{}) error { return json.Unmarshal(r.Body, v) } ``` ### Example 2: Middleware-Based HTTP Server (Node.js) ```javascript const http = require('http'); class HTTPMiddleware { constructor() { this.middleware = []; } use(fn) { this.middleware.push(fn); return this; } async handle(request, response) { const context = { request, response, state: {}, status: 200 }; for (const fn of this.middleware) { await fn(context); if (context.response.writableEnded) { return; } if (context.status >= 400) { break; } } if (!context.response.writableEnded) { response.statusCode = context.status; response.end(JSON.stringify(context.body || {})); } } } class HTTPServer { constructor(port = 3000) { this.port = port; this.middleware = new HTTPMiddleware(); this.routes = new Map(); this.server = null; } use(fn) { this.middleware.use(fn); return this; } get(path, handler) { this.addRoute('GET', path, handler); return this; } post(path, handler) { this.addRoute('POST', path, handler); return this; } put(path, handler) { this.addRoute('PUT', path, handler); return this; } delete(path, handler) { this.addRoute('DELETE', path, handler); return this; } addRoute(method, path, handler) { const pattern = this.pathToRegex(path); this.routes.set(`${method}:${pattern}`, { pattern, handler }); } pathToRegex(path) { const pattern = path .replace(/\//g, '\\/') .replace(/:(\w+)/g, '(?<$1>[^/]+)'); return new RegExp(`^${pattern}$`); } findRoute(method, path) { for (const [key, route] of this.routes) { const [routeMethod, pattern] = key.split(':'); if (routeMethod !== method) continue; const match = path.match(pattern); if (match) { return { handler: route.handler, params: match.groups }; } } return null; } async handleRequest(req, res) { const url = new URL(req.url, `http://${req.headers.host}`); const route = this.findRoute(req.method, url.pathname); const context = { request: req, response: res, method: req.method, path: url.pathname, query: Object.fromEntries(url.searchParams), params: route?.params || {}, body: null, status: 200 }; // Parse body for POST/PUT if (['POST', 'PUT', 'PATCH'].includes(req.method)) { await this.parseBody(req); context.body = req.body; } // Run middleware for (const fn of this.middleware.middleware) { await fn(context); if (res.writableEnded) return; } // Handle route if (route) { await route.handler(context); } else { context.status = 404; context.body = { error: 'Not Found' }; res.statusCode = 404; res.end(JSON.stringify(context.body)); } } async parseBody(req) { return new Promise((resolve) => { let body = ''; req.on('data', chunk => body += chunk.toString()); req.on('end', () => { try { req.body = JSON.parse(body || '{}'); } catch { req.body = {}; } resolve(); }); }); } listen() { this.server = http.createServer(this.handleRequest.bind(this)); this.server.listen(this.port, () => { console.log(`Server running on port ${this.port}`); }); return this.server; } } // Usage const app = new HTTPServer(3000); // Logging middleware app.use(async (ctx) => { console.log(`${ctx.method} ${ctx.path} - ${ctx.status}`); }); // CORS middleware app.use(async (ctx) => { ctx.response.setHeader('Access-Control-Allow-Origin', '*'); ctx.response.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE'); ctx.response.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); }); // Auth middleware app.use(async (ctx) => { const auth = ctx.request.headers.authorization; if (!auth && !ctx.request.url.includes('/public')) { ctx.status = 401; ctx.body = { error: 'Unauthorized' }; } }); // Routes app.get('/api/users', async (ctx) => { ctx.body = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]; }); app.get('/api/users/:id', async (ctx) => { ctx.body = { id: ctx.params.id, name: `User ${ctx.params.id}` }; });
View on GitHub
This SKILL.md is very large, so SkillsMP previews the first section here. View on GitHub