mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 10:12:29 +08:00 
			
		
		
		
	caddyhttp: Merge query matchers in Caddyfile (#3839)
Also, turns out that `Add` on headers will work even if there's nothing there yet, so we can remove the condition I introduced in #3832
This commit is contained in:
		@ -25,6 +25,12 @@
 | 
				
			|||||||
		header Bar foo
 | 
							header Bar foo
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	respond @matcher7 "header matcher merging values of the same field"
 | 
						respond @matcher7 "header matcher merging values of the same field"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@matcher8 {
 | 
				
			||||||
 | 
							query foo=bar foo=baz bar=foo
 | 
				
			||||||
 | 
							query bar=baz
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						respond @matcher8 "query matcher merging pairs with the same keys"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
----------
 | 
					----------
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@ -155,6 +161,28 @@
 | 
				
			|||||||
									"handler": "static_response"
 | 
														"handler": "static_response"
 | 
				
			||||||
								}
 | 
													}
 | 
				
			||||||
							]
 | 
												]
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
											{
 | 
				
			||||||
 | 
												"match": [
 | 
				
			||||||
 | 
													{
 | 
				
			||||||
 | 
														"query": {
 | 
				
			||||||
 | 
															"bar": [
 | 
				
			||||||
 | 
																"foo",
 | 
				
			||||||
 | 
																"baz"
 | 
				
			||||||
 | 
															],
 | 
				
			||||||
 | 
															"foo": [
 | 
				
			||||||
 | 
																"bar",
 | 
				
			||||||
 | 
																"baz"
 | 
				
			||||||
 | 
															]
 | 
				
			||||||
 | 
														}
 | 
				
			||||||
 | 
													}
 | 
				
			||||||
 | 
												],
 | 
				
			||||||
 | 
												"handle": [
 | 
				
			||||||
 | 
													{
 | 
				
			||||||
 | 
														"body": "query matcher merging pairs with the same keys",
 | 
				
			||||||
 | 
														"handler": "static_response"
 | 
				
			||||||
 | 
													}
 | 
				
			||||||
 | 
												]
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
					]
 | 
										]
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
				
			|||||||
@ -353,10 +353,7 @@ func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
 | 
				
			|||||||
		*m = make(map[string][]string)
 | 
							*m = make(map[string][]string)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for d.Next() {
 | 
						for d.Next() {
 | 
				
			||||||
		var query string
 | 
							for _, query := range d.RemainingArgs() {
 | 
				
			||||||
		if !d.Args(&query) {
 | 
					 | 
				
			||||||
			return d.ArgErr()
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
			if query == "" {
 | 
								if query == "" {
 | 
				
			||||||
				continue
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@ -364,7 +361,8 @@ func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
 | 
				
			|||||||
			if len(parts) != 2 {
 | 
								if len(parts) != 2 {
 | 
				
			||||||
				return d.Errf("malformed query matcher token: %s; must be in param=val format", d.Val())
 | 
									return d.Errf("malformed query matcher token: %s; must be in param=val format", d.Val())
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		url.Values(*m).Set(parts[0], parts[1])
 | 
								url.Values(*m).Add(parts[0], parts[1])
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		if d.NextBlock(0) {
 | 
							if d.NextBlock(0) {
 | 
				
			||||||
			return d.Err("malformed query matcher: blocks are not supported")
 | 
								return d.Err("malformed query matcher: blocks are not supported")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@ -411,12 +409,7 @@ func (m *MatchHeader) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		// If multiple header matchers with the same header field are defined,
 | 
							// If multiple header matchers with the same header field are defined,
 | 
				
			||||||
		// we want to add the existing to the list of headers (will be OR'ed)
 | 
							// we want to add the existing to the list of headers (will be OR'ed)
 | 
				
			||||||
		existing := http.Header(*m).Values(field)
 | 
					 | 
				
			||||||
		if len(existing) > 0 {
 | 
					 | 
				
			||||||
		http.Header(*m).Add(field, val)
 | 
							http.Header(*m).Add(field, val)
 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			http.Header(*m).Set(field, val)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if d.NextBlock(0) {
 | 
							if d.NextBlock(0) {
 | 
				
			||||||
			return d.Err("malformed header matcher: blocks are not supported")
 | 
								return d.Err("malformed header matcher: blocks are not supported")
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user