mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 10:12:29 +08:00 
			
		
		
		
	httpcaddyfile: Fix panic when parsing route with matchers (#3746)
Fixes #3745
This commit is contained in:
		@ -304,13 +304,17 @@ func parseSegmentAsConfig(h Helper) ([]ConfigValue, error) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// find and extract any embedded matcher definitions in this scope
 | 
							// find and extract any embedded matcher definitions in this scope
 | 
				
			||||||
		for i, seg := range segments {
 | 
							for i := 0; i < len(segments); i++ {
 | 
				
			||||||
 | 
								seg := segments[i]
 | 
				
			||||||
			if strings.HasPrefix(seg.Directive(), matcherPrefix) {
 | 
								if strings.HasPrefix(seg.Directive(), matcherPrefix) {
 | 
				
			||||||
 | 
									// parse, then add the matcher to matcherDefs
 | 
				
			||||||
				err := parseMatcherDefinitions(caddyfile.NewDispenser(seg), matcherDefs)
 | 
									err := parseMatcherDefinitions(caddyfile.NewDispenser(seg), matcherDefs)
 | 
				
			||||||
				if err != nil {
 | 
									if err != nil {
 | 
				
			||||||
					return nil, err
 | 
										return nil, err
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
									// remove the matcher segment (consumed), then step back the loop
 | 
				
			||||||
				segments = append(segments[:i], segments[i+1:]...)
 | 
									segments = append(segments[:i], segments[i+1:]...)
 | 
				
			||||||
 | 
									i--
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										31
									
								
								caddytest/integration/caddyfile_adapt/matchers_in_route.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								caddytest/integration/caddyfile_adapt/matchers_in_route.txt
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,31 @@
 | 
				
			|||||||
 | 
					:80 {
 | 
				
			||||||
 | 
					    route {
 | 
				
			||||||
 | 
					        # unused matchers should not panic
 | 
				
			||||||
 | 
					        # see https://github.com/caddyserver/caddy/issues/3745
 | 
				
			||||||
 | 
					        @matcher1 path /path1
 | 
				
			||||||
 | 
					        @matcher2 path /path2
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					----------
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						"apps": {
 | 
				
			||||||
 | 
							"http": {
 | 
				
			||||||
 | 
								"servers": {
 | 
				
			||||||
 | 
									"srv0": {
 | 
				
			||||||
 | 
										"listen": [
 | 
				
			||||||
 | 
											":80"
 | 
				
			||||||
 | 
										],
 | 
				
			||||||
 | 
										"routes": [
 | 
				
			||||||
 | 
											{
 | 
				
			||||||
 | 
												"handle": [
 | 
				
			||||||
 | 
													{
 | 
				
			||||||
 | 
														"handler": "subroute"
 | 
				
			||||||
 | 
													}
 | 
				
			||||||
 | 
												]
 | 
				
			||||||
 | 
											}
 | 
				
			||||||
 | 
										]
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user