001package io.prometheus.metrics.exporter.common; 002 003import io.prometheus.metrics.model.registry.PrometheusScrapeRequest; 004import java.io.UnsupportedEncodingException; 005import java.net.URLDecoder; 006import java.util.ArrayList; 007import java.util.Enumeration; 008 009public interface PrometheusHttpRequest extends PrometheusScrapeRequest { 010 011 /** See {@code jakarta.servlet.http.HttpServletRequest.getQueryString()} */ 012 String getQueryString(); 013 014 /** See {@code jakarta.servlet.http.HttpServletRequest.getHeaders(String)} */ 015 Enumeration<String> getHeaders(String name); 016 017 /** See {@code jakarta.servlet.http.HttpServletRequest.getMethod()} */ 018 String getMethod(); 019 020 /** See {@code jakarta.servlet.http.HttpServletRequest.getHeader(String)} */ 021 default String getHeader(String name) { 022 Enumeration<String> headers = getHeaders(name); 023 if (headers == null || !headers.hasMoreElements()) { 024 return null; 025 } else { 026 return headers.nextElement(); 027 } 028 } 029 030 /** See {@code jakarta.servlet.ServletRequest.getParameter(String)} */ 031 default String getParameter(String name) { 032 String[] values = getParameterValues(name); 033 if (values == null || values.length == 0) { 034 return null; 035 } else { 036 return values[0]; 037 } 038 } 039 040 /** See {@code jakarta.servlet.ServletRequest.getParameterValues(String)} */ 041 @Override 042 default String[] getParameterValues(String name) { 043 try { 044 ArrayList<String> result = new ArrayList<>(); 045 String queryString = getQueryString(); 046 if (queryString != null) { 047 String[] pairs = queryString.split("&"); 048 for (String pair : pairs) { 049 int idx = pair.indexOf("="); 050 if (idx != -1 && URLDecoder.decode(pair.substring(0, idx), "UTF-8").equals(name)) { 051 result.add(URLDecoder.decode(pair.substring(idx + 1), "UTF-8")); 052 } 053 } 054 } 055 if (result.isEmpty()) { 056 // Servlet API: getParameterValues() returns null if the parameter does not exist. 057 return null; 058 } else { 059 return result.toArray(new String[0]); 060 } 061 } catch (UnsupportedEncodingException e) { 062 // UTF-8 encoding not supported. 063 throw new RuntimeException(e); 064 } 065 } 066}