package pkg import ( "errors" "strconv" "strings" ) // HTTPCompressionProfile : Virtual server HTTP compression profile configuration type HTTPCompressionProfile struct { ProfileCommon AllowHTTP10 string `json:"allow_http_10"` //allow-http-10 enabled BrowserWorkarounds string `json:"browser_workarounds"` //browser-workarounds disabled BufferSize int `json:"buffer_size"` //buffer-size 131072 ContentTypeExclude []string `json:"content_type_exclude"` //content-type-exclude none ContentTypeInclude []string `json:"content_type_include"` //content-type-include { text/ "application/(xml|x-javascript)" } CPUSaver string `json:"cpu_saver"` //cpu-saver enabled CPUSaverHigh int `json:"cpu_saver_high"` //cpu-saver-high 90 CPUSaverLow int `json:"cpu_saver_low"` //cpu-saver-low 75 GZIPLevel int `json:"gzip_level"` //gzip-level 1 GZIPMemoryLevel string `json:"gzip_memory_level"` //gzip-memory-level 16k GZIPWindowSize string `json:"gzip_window_size"` //gzip-window-size 64k KeepAcceptEncoding string `json:"keep_accept_encoding"` //keep-accept-encoding disabled MethodPrefer string `json:"method_prefer"` //method-prefer gzip MinSize int `json:"min_size"` //min-size 1024 Selective string `json:"selective"` //selective disabled URIExclude []string `json:"uri_exclude"` //uri-exclude none URIInclude []string `json:"uri_include"` //uri-include { .* } VaryHeader string `json:"vary_header"` //vary-header enabled } func ParseHTTPCompression(raw string) (*HTTPCompressionProfile, error) { p := new(HTTPCompressionProfile) p.ContentTypeExclude = []string{} p.ContentTypeInclude = []string{} p.URIExclude = []string{} p.URIInclude = []string{} split := strings.Split(raw, "\n") if !strings.Contains(split[0], "ltm profile http-compression") { return nil, errors.New("not an http-compression profile") } p.ProfileCommon = parseCommon(raw) for _, line := range split { f := strings.Fields(line) switch strings.ToLower(f[0]) { case "allow-http-10": p.AllowHTTP10 = strings.Join(f[1:], " ") case "browser-workarounds": p.BrowserWorkarounds = strings.Join(f[1:], " ") case "buffer-size": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.BufferSize = i case "content-type-exclude": if f[1] != "none" { p.ContentTypeExclude = f[2 : len(f)-1] } case "content-type-include": if f[1] != "none" { p.ContentTypeInclude = f[2 : len(f)-1] } case "cpu-saver": p.CPUSaver = strings.Join(f[1:], " ") case "cpu-saver-high": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.CPUSaverHigh = i case "cpu-saver-low": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.CPUSaverLow = i case "gzip-level": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.GZIPLevel = i case "gzip-memory-level": p.GZIPMemoryLevel = strings.Join(f[1:], " ") case "gzip-window-size": p.GZIPWindowSize = strings.Join(f[1:], " ") case "keep-accept-encoding": p.KeepAcceptEncoding = strings.Join(f[1:], " ") case "method-prefer": p.MethodPrefer = strings.Join(f[1:], " ") case "min-size": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.MinSize = i case "selective": p.Selective = strings.Join(f[1:], " ") case "uri-exclude": if f[1] != "none" { p.URIExclude = append(p.URIExclude, f[2:len(f)-1]...) } case "uri-include": if f[1] != "none" { p.URIInclude = append(p.URIInclude, f[2:len(f)-1]...) } case "vary-header": p.VaryHeader = strings.Join(f[1:], " ") } } return p, nil }