一个HTTP分隔符统治它们全部

huangapple go评论73阅读模式
英文:

One HTTP Delimiter to Rule Them All

问题

我有一个格式为blah = foo的配置文件。我想要有以下格式的条目:

http = https://stackoverflow.com/questions,header keys and values,string to search for.

我可以要求URL进行URL编码。在上面的示例中(在=号后拆分一次之后),有没有任何ASCII字符可以使用,以确保它在任何位置都不是有效的值?我的示例中使用了逗号,但我认为在标头值中逗号是有效的?

在查阅了一些RFC之后,我发现如果有人对此更熟悉,可以帮我节省一些麻烦。

另外,我的项目使用Go语言,如果有现有的标准库可以帮助处理这个问题,那就更好了...

英文:

I have a configuration file in the format of blah = foo. I would like to have entries like:

http = https://stackoverflow.com/questions,header keys and values,string to search for.

I'm okay requiring that the the url be urlecncoded. Is there any ASCII character I can use that won't be valid value anywhere in the above example (After splitting once on =)? My example uses a comma but I think that is valid in a header value?

After pouring through some RFCs I figure someone is more familiar with this can save me some pain.

Also my project is in Go if there are existing std library that might help with this...

答案1

得分: 1

你可以使用非ASCII字符并进行URL编码,例如在Linux上使用中间点(compose + ^ + .):

const sep = `·`
const t = `http = https://stackoverflow.com/questions·string to search for·header=value·header=value`

func parseLine(line string) (name, url, search string, headers []string) {
    idx := strings.Index(line, " = ")
    if idx == -1 {
        return
    }
    name = line[:idx]
    parts := strings.Split(line[idx+3:], sep)
    if len(parts) < 3 {
        // 处理无效的行
    }
    url, search = parts[0], parts[1]
    headers = parts[2:]
    return
}

不过,使用JSON可能是最好且最易于长期维护的选项。

为了完整起见,JSON版本如下所示:

type Site struct {
    Url     string
    Query   string
    Headers map[string]string
}

const t = `[
    {
        "url": "https://stackoverflow.com/questions",
        "query": "string to search for",
        "headers": {"header": "value", "header2": "value"}
    },
    {
        "url": "https://google.com",
        "query": "string to search for",
        "headers": {"header": "value", "header2": "value"}
    }
]`

func main() {
    var sites []Site
    err := json.Unmarshal([]byte(t), &sites)
    fmt.Printf("%+v (%v)\n", sites, err)
}
英文:

You can use a non-ascii character and urlencode, for example using the middle dot (compose + ^ + . on linux):

const sep = `&#183;`
const t = `http = https://stackoverflow.com/questions&#183;string to search for&#183;header=value&#183;header=value`

func parseLine(line string) (name, url, search string, headers []string) {
	idx := strings.Index(line, &quot; = &quot;)
	if idx == -1 {
		return
	}
	name = line[:idx]
	parts := strings.Split(line[idx+3:], sep)
	if len(parts) &lt; 3 {
		// handle invalid line
	}
	url, search = parts[0], parts[1]
	headers = parts[2:]
	return
}

Although, using JSON is probably the best and most long-term maintainable option.

For completeness sake, a json version would look like:

type Site struct {
	Url     string
	Query   string
	Headers map[string]string
}

const t = `[
	{
		&quot;url&quot;: &quot;https://stackoverflow.com/questions&quot;,
		&quot;query&quot;: &quot;string to search for&quot;,
		&quot;headers&quot;: {&quot;header&quot;: &quot;value&quot;, &quot;header2&quot;: &quot;value&quot;}
	},
	{
		&quot;url&quot;: &quot;https://google.com&quot;,
		&quot;query&quot;: &quot;string to search for&quot;,
		&quot;headers&quot;: {&quot;header&quot;: &quot;value&quot;, &quot;header2&quot;: &quot;value&quot;}
	}
]`

func main() {
	var sites []Site
	err := json.Unmarshal([]byte(t), &amp;sites)
	fmt.Printf(&quot;%+v (%v)\n&quot;, sites, err)
}

答案2

得分: 0

基本上,你需要查看RFC 3986、RFC 7230等相关文档,了解可能发生的情况。

如果你坚持要求URI有效,那么它们就很简单,只需使用空格字符或者"<"和">"作为分隔符。

字段值可以是几乎任何内容;但是HTTP禁止控制字符,所以你可能可以使用水平制表符(如果你不介意使用无效的字段值而陷入麻烦)。

英文:

Essentially you have to look at RFC 3986, RFC 7230 and friends to see what can occur.

URIs are simple if you insist on them to be valid, just use the space character or "<" and ">" as delimiters.

Field values can be almost anything; HTTP forbids control characters though, so you might be able to use horizontal TABs (if you're ok with getting into trouble with invalid field values).

huangapple
  • 本文由 发表于 2014年10月6日 02:44:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/26205987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定