har

package module
v0.0.0-...-a736c1b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2026 License: MIT Imports: 1 Imported by: 0

README

Go-HAR

Go Reference Go Report Card License

Go-HAR 是一个高性能、灵活的 HTTP Archive (HAR) 解析和处理库,用 Go 语言实现。它为处理 HAR 文件提供了多种策略,从简单的小型文件到需要优化内存使用的大型文件都能高效处理。

特性

  • 多种解析策略

    • 标准解析:适用于常规场景
    • 内存优化:减少大型 HAR 文件的内存占用
    • 懒加载:延迟加载大型内容字段
    • 流式处理:逐条处理超大 HAR 文件
  • 灵活的接口设计

    • 基于接口的设计,支持不同实现之间的互操作
    • 统一的 API,无论使用哪种解析策略
  • 增强的错误处理

    • 详细的错误信息和上下文
    • 部分解析能力和警告收集
  • 高级功能

    • 高效过滤和搜索
    • 丰富的统计分析
    • 可视化和报告生成

安装

go get github.com/cyberspacesec/go-har

快速开始

基本用法
package main

import (
	"fmt"
	"log"
	
	"github.com/cyberspacesec/go-har"
)

func main() {
	// 解析 HAR 文件
	harData, err := har.ParseHarFile("example.har")
	if err != nil {
		log.Fatalf("无法解析 HAR 文件: %v", err)
	}
	
	// 访问 HAR 数据
	fmt.Printf("HAR 版本: %s\n", harData.Log.Version)
	fmt.Printf("条目数量: %d\n", len(harData.Log.Entries))
	
	// 遍历所有请求
	for i, entry := range harData.Log.Entries {
		fmt.Printf("请求 #%d: %s %s\n", i+1, entry.Request.Method, entry.Request.URL)
	}
}
内存优化模式
// 使用内存优化模式处理大型文件
harData, err := har.ParseHarFile("large.har", har.WithMemoryOptimized())
if err != nil {
	log.Fatalf("无法解析 HAR 文件: %v", err)
}

// 接口保持一致,使用方式相同
for _, entry := range harData.GetEntries() {
	fmt.Printf("URL: %s\n", entry.GetRequest().GetURL())
}
懒加载模式
// 使用懒加载模式延迟加载大型内容
harData, err := har.ParseHarFile("large_content.har", har.WithLazyLoading())
if err != nil {
	log.Fatalf("无法解析 HAR 文件: %v", err)
}

// 基本信息直接可用,大型内容仅在需要时加载
for _, entry := range harData.GetEntries() {
	resp := entry.GetResponse()
	fmt.Printf("状态码: %d, 内容大小: %d\n", 
		resp.GetStatus(), 
		resp.GetContent().GetSize())
	
	// 内容仅在需要时加载
	if resp.GetStatus() == 200 {
		content := resp.GetContent()
		text := content.GetText() // 此时才加载内容
		fmt.Printf("内容长度: %d\n", len(text))
	}
}

高级用法

查看 详细文档 了解更多高级功能,包括:

  • 流式解析超大 HAR 文件
  • 增强的错误处理
  • 过滤和搜索功能
  • 统计分析和可视化
  • 命令行工具

项目结构

  • pkg/har/ - 核心 HAR 解析和处理代码
  • examples/ - 示例代码和实用工具
    • examples/statistics/ - 统计分析示例
    • examples/visualization/ - 可视化示例
    • examples/cli-tool/ - 命令行工具示例
  • doc/ - 详细文档
    • doc/usage.md - 使用文档
    • doc/structure.md - HAR 结构文档

贡献

欢迎贡献!请查看 贡献指南 了解如何参与项目开发。

许可证

本项目使用 MIT 许可证

Documentation

Overview

Package har provides functionality for parsing and manipulating HAR (HTTP Archive) files. This is a compatibility wrapper that forwards to the implementation in pkg/har.

Index

Constants

View Source
const (
	MethodUnknown = har.MethodUnknown
	MethodGET     = har.MethodGET
	MethodPOST    = har.MethodPOST
	MethodPUT     = har.MethodPUT
	MethodDELETE  = har.MethodDELETE
	MethodHEAD    = har.MethodHEAD
	MethodOPTIONS = har.MethodOPTIONS
	MethodPATCH   = har.MethodPATCH
	MethodCONNECT = har.MethodCONNECT
	MethodTRACE   = har.MethodTRACE
)

HTTP Method constants

View Source
const (
	FormatCSV      = har.FormatCSV
	FormatMarkdown = har.FormatMarkdown
	FormatHTML     = har.FormatHTML
	FormatText     = har.FormatText
)

Format constants

View Source
const (
	ErrCodeUnknown       = har.ErrCodeUnknown
	ErrCodeFileSystem    = har.ErrCodeFileSystem
	ErrCodeJSONParse     = har.ErrCodeJSONParse
	ErrCodeInvalidFormat = har.ErrCodeInvalidFormat
	ErrCodeValidation    = har.ErrCodeValidation
	ErrCodeMissingField  = har.ErrCodeMissingField
	ErrCodeInvalidValue  = har.ErrCodeInvalidValue
	ErrCodeUnsupported   = har.ErrCodeUnsupported
)

Error code constants

Variables

View Source
var (
	// Basic operations
	ParseHarFile = har.ParseHarFile
	ParseHar     = har.ParseHar
	NewHar       = har.NewHar

	// Optimized parsing
	ParseHarFileOptimized = har.ParseHarFileOptimized
	ParseHarOptimized     = har.ParseHarOptimized
	ToOptimizedHar        = har.ToOptimizedHar

	// Lazy loading
	ParseHarWithLazyLoading     = har.ParseHarWithLazyLoading
	ParseHarFileWithLazyLoading = har.ParseHarFileWithLazyLoading

	// Streaming
	NewStreamingHarFromFile = har.NewStreamingHarFromFile

	// Enhanced parsing
	ParseHarWithOptions      = har.ParseHarWithOptions
	ParseHarFileWithOptions  = har.ParseHarFileWithOptions
	ParseHarEnhanced         = har.ParseHarEnhanced
	ParseHarFileEnhanced     = har.ParseHarFileEnhanced
	ParseHarLenient          = har.ParseHarLenient
	ParseHarFileLenient      = har.ParseHarFileLenient
	ParseHarWithWarnings     = har.ParseHarWithWarnings
	ParseHarFileWithWarnings = har.ParseHarFileWithWarnings
	DefaultParseOptions      = har.DefaultParseOptions

	// Error utilities
	NewHarError            = har.NewHarError
	NewFileSystemError     = har.NewFileSystemError
	NewJSONParseError      = har.NewJSONParseError
	WrapJSONUnmarshalError = har.WrapJSONUnmarshalError
	NewValidationError     = har.NewValidationError
	NewInvalidFormatError  = har.NewInvalidFormatError
	NewMissingFieldError   = har.NewMissingFieldError
	NewInvalidValueError   = har.NewInvalidValueError
	NewUnsupportedError    = har.NewUnsupportedError

	// Utilities
	ParseMethod           = har.ParseMethod
	DefaultConvertOptions = har.DefaultConvertOptions

	// 新的函数选项模式API
	Parse                      = har.Parse
	ParseFile                  = har.ParseFile
	NewStreamingParser         = har.NewStreamingParser
	NewStreamingParserFromFile = har.NewStreamingParserFromFile

	// 选项函数
	WithLenient         = har.WithLenient
	WithSkipValidation  = har.WithSkipValidation
	WithCollectWarnings = har.WithCollectWarnings
	WithMaxWarnings     = har.WithMaxWarnings
	WithMemoryOptimized = har.WithMemoryOptimized
	WithLazyLoading     = har.WithLazyLoading
	WithStreaming       = har.WithStreaming

	// 预定义选项组
	OptMemoryEfficient = har.OptMemoryEfficient
	OptFast            = har.OptFast
	OptLenient         = har.OptLenient
	OptPerformance     = har.OptPerformance
)

Forward all functions

Functions

This section is empty.

Types

type AfterRequest

type AfterRequest = har.AfterRequest

AfterRequest represents cache state after request

type BeforeRequest

type BeforeRequest = har.BeforeRequest

BeforeRequest represents cache state before request

type Browser

type Browser = har.Browser

Browser represents browser information

type Cache

type Cache = har.Cache

Cache represents cache information

type CallFrame

type CallFrame = har.CallFrame

CallFrame represents a frame in a call stack

type Content

type Content = har.Content

Content represents response content

type ContentProvider

type ContentProvider = har.ContentProvider

Error types

type ConvertFormat

type ConvertFormat = har.ConvertFormat

ConvertFormat for conversion formats

type ConvertOptions

type ConvertOptions = har.ConvertOptions

Error types

type Cookie = har.Cookie

Cookie represents an HTTP cookie

type CookieProvider

type CookieProvider = har.CookieProvider

Error types

type Creator

type Creator = har.Creator

Creator represents the creator information

type Entries

type Entries = har.Entries

Entries represents an entry in the HAR file

type EntryIterator

type EntryIterator = har.EntryIterator

Error types

type EntryProvider

type EntryProvider = har.EntryProvider

Error types

type ErrorCode

type ErrorCode = har.ErrorCode

Error types

type FilterOptions

type FilterOptions = har.FilterOptions

Error types

type FilterResult

type FilterResult = har.FilterResult

Error types

type HARProvider

type HARProvider = har.HARProvider

接口类型

type HTTPMethod

type HTTPMethod = har.HTTPMethod

HTTPMethod enum type for HTTP methods

type Har

type Har = har.Har

Har represents a HAR file

type HarError

type HarError = har.HarError

Error types

type HeaderProvider

type HeaderProvider = har.HeaderProvider

Error types

type Headers

type Headers = har.Headers

Headers represents HTTP headers

type Initiator

type Initiator = har.Initiator

Initiator represents the initiator of a request

type LazyContent

type LazyContent = har.LazyContent

Error types

type LazyEntries

type LazyEntries = har.LazyEntries

Error types

type LazyHar

type LazyHar = har.LazyHar

Error types

type LazyResponse

type LazyResponse = har.LazyResponse

Error types

type Log

type Log = har.Log

Log represents the log section of a HAR file

type OptimizedContent

type OptimizedContent = har.OptimizedContent

Error types

type OptimizedEntries

type OptimizedEntries = har.OptimizedEntries

Error types

type OptimizedHar

type OptimizedHar = har.OptimizedHar

Error types

type OptimizedRequest

type OptimizedRequest = har.OptimizedRequest

Error types

type OptimizedResponse

type OptimizedResponse = har.OptimizedResponse

Error types

type OptimizedTimings

type OptimizedTimings = har.OptimizedTimings

Error types

type Option

type Option = har.Option

选项类型

type PageProvider

type PageProvider = har.PageProvider

Error types

type PageTimings

type PageTimings = har.PageTimings

PageTimings represents page timing information

type PageTimingsProvider

type PageTimingsProvider = har.PageTimingsProvider

Error types

type Pages

type Pages = har.Pages

Pages represents a page in the HAR file

type Parent

type Parent = har.Parent

Parent represents parent information in a call stack

type ParentID

type ParentID = har.ParentID

ParentID represents a parent ID in a call stack

type ParseOptions

type ParseOptions = har.ParseOptions

Error types

type Request

type Request = har.Request

Request represents an HTTP request

type RequestProvider

type RequestProvider = har.RequestProvider

Error types

type Response

type Response = har.Response

Response represents an HTTP response

type ResponseProvider

type ResponseProvider = har.ResponseProvider

Error types

type Result

type Result = har.Result

Error types

type Stack

type Stack = har.Stack

Stack represents a call stack

type StreamingEntryIterator

type StreamingEntryIterator = har.StreamingEntryIterator

Error types

type StreamingHar

type StreamingHar = har.StreamingHar

Error types

type Timings

type Timings = har.Timings

Timings represents timing information for a request

type TimingsProvider

type TimingsProvider = har.TimingsProvider

Error types

Directories

Path Synopsis
advanced command
cli-tool command
error_handling command
performance command
statistics command
visualization command
pkg
har

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL