|
| 1 | +/* |
| 2 | + * Zed Attack Proxy (ZAP) and its related class files. |
| 3 | + * |
| 4 | + * ZAP is an HTTP/HTTPS proxy for assessing web application security. |
| 5 | + * |
| 6 | + * Copyright 2025 The ZAP Development Team |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | +package org.zaproxy.clientapi.core; |
| 21 | + |
| 22 | +/* |
| 23 | + * Zed Attack Proxy (ZAP) and its related class files. |
| 24 | + * |
| 25 | + * ZAP is an HTTP/HTTPS proxy for assessing web application security. |
| 26 | + * |
| 27 | + * Copyright 2011 The ZAP Development Team |
| 28 | + * |
| 29 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 30 | + * you may not use this file except in compliance with the License. |
| 31 | + * You may obtain a copy of the License at |
| 32 | + * |
| 33 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 34 | + * |
| 35 | + * Unless required by applicable law or agreed to in writing, software |
| 36 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 37 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 38 | + * See the License for the specific language governing permissions and |
| 39 | + * limitations under the License. |
| 40 | + */ |
| 41 | + |
| 42 | +import java.net.MalformedURLException; |
| 43 | +import java.net.URI; |
| 44 | +import java.net.URISyntaxException; |
| 45 | +import java.net.URL; |
| 46 | +import java.net.URLEncoder; |
| 47 | +import java.nio.charset.StandardCharsets; |
| 48 | +import java.util.Map; |
| 49 | + |
| 50 | +/** |
| 51 | + * Builds HTTP requests for the ZAP API. |
| 52 | + * |
| 53 | + * <p>This class is responsible only for request construction (URL, query parameters, body, |
| 54 | + * headers). |
| 55 | + * |
| 56 | + * <p>No networking or I/O is performed here. |
| 57 | + */ |
| 58 | +final class ZapRequestBuilder { |
| 59 | + |
| 60 | + private static final String ZAP_API_HOST = "http://zap/"; |
| 61 | + |
| 62 | + private ZapRequestBuilder() { |
| 63 | + // utility class |
| 64 | + } |
| 65 | + |
| 66 | + static HttpRequest buildZapRequest( |
| 67 | + String requestMethod, |
| 68 | + String format, |
| 69 | + String component, |
| 70 | + String type, |
| 71 | + String method, |
| 72 | + Map<String, String> params, |
| 73 | + String apiKey) |
| 74 | + throws MalformedURLException, URISyntaxException { |
| 75 | + |
| 76 | + StringBuilder urlBuilder = new StringBuilder(); |
| 77 | + urlBuilder |
| 78 | + .append(ZAP_API_HOST) |
| 79 | + .append(format) |
| 80 | + .append('/') |
| 81 | + .append(component) |
| 82 | + .append('/') |
| 83 | + .append(type) |
| 84 | + .append('/') |
| 85 | + .append(method) |
| 86 | + .append('/'); |
| 87 | + |
| 88 | + String body = null; |
| 89 | + |
| 90 | + if (params != null && !params.isEmpty()) { |
| 91 | + if (HttpRequest.GET_METHOD.equals(requestMethod)) { |
| 92 | + urlBuilder.append('?'); |
| 93 | + appendParams(params, urlBuilder); |
| 94 | + } else { |
| 95 | + body = appendParams(params, new StringBuilder()).toString(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + HttpRequest request = |
| 100 | + new HttpRequest(requestMethod, createUrl(urlBuilder.toString()), body); |
| 101 | + |
| 102 | + if (apiKey != null && !apiKey.isEmpty()) { |
| 103 | + request.addHeader("X-ZAP-API-Key", apiKey); |
| 104 | + } |
| 105 | + |
| 106 | + return request; |
| 107 | + } |
| 108 | + |
| 109 | + private static URL createUrl(String value) throws MalformedURLException, URISyntaxException { |
| 110 | + return new URI(value).toURL(); |
| 111 | + } |
| 112 | + |
| 113 | + private static StringBuilder appendParams(Map<String, String> params, StringBuilder sb) { |
| 114 | + |
| 115 | + for (Map.Entry<String, String> p : params.entrySet()) { |
| 116 | + sb.append(encode(p.getKey())).append('='); |
| 117 | + if (p.getValue() != null) { |
| 118 | + sb.append(encode(p.getValue())); |
| 119 | + } |
| 120 | + sb.append('&'); |
| 121 | + } |
| 122 | + return sb; |
| 123 | + } |
| 124 | + |
| 125 | + private static String encode(String value) { |
| 126 | + return URLEncoder.encode(value, StandardCharsets.UTF_8); |
| 127 | + } |
| 128 | +} |
0 commit comments