| 132 | |
| 133 | |
| 134 | static int h2_send_response(struct sip_msg *msg, int *code, |
| 135 | str *headers_json, str *body) |
| 136 | { |
| 137 | #define H_STATUS ":status" |
| 138 | cJSON *hdrs = NULL, *it; |
| 139 | struct h2_response *r; |
| 140 | int nh = 1; |
| 141 | |
| 142 | if (!h2_response) |
| 143 | return -1; |
| 144 | r = *h2_response; |
| 145 | r->code = -1; |
| 146 | |
| 147 | if (*code < 100 || *code > 599) { |
| 148 | LM_ERR("invalid HTTP/2 response code: %d, must be 100-599\n", *code); |
| 149 | goto error; |
| 150 | } |
| 151 | |
| 152 | if (headers_json) { |
| 153 | char *hp; |
| 154 | str h; |
| 155 | |
| 156 | /* safe to dereference outside buff (still within PKG block) */ |
| 157 | if (headers_json->s[headers_json->len] != '\0') { |
| 158 | if (pkg_nt_str_dup(&h, headers_json) != 0) { |
| 159 | LM_ERR("oom\n"); |
| 160 | goto error; |
| 161 | } |
| 162 | hp = h.s; |
| 163 | } else { |
| 164 | hp = headers_json->s; |
| 165 | } |
| 166 | |
| 167 | hdrs = cJSON_Parse(hp); |
| 168 | if (hp != headers_json->s) |
| 169 | pkg_free(hp); |
| 170 | |
| 171 | if (!hdrs) { |
| 172 | LM_ERR("failed to parse 'headers_json' (bad JSON syntax)\n"); |
| 173 | LM_ERR("first %d characters: %.*s ...\n", |
| 174 | headers_json->len > 20 ? 20 : headers_json->len, |
| 175 | headers_json->len > 20 ? 20 : headers_json->len, headers_json->s); |
| 176 | goto error; |
| 177 | } |
| 178 | |
| 179 | if (hdrs->type != cJSON_Array) { |
| 180 | LM_ERR("bad 'headers_json' value (must be a List of name/value pairs)\n"); |
| 181 | LM_ERR("first %d characters: %.*s ...\n", |
| 182 | headers_json->len > 20 ? 20 : headers_json->len, |
| 183 | headers_json->len > 20 ? 20 : headers_json->len, headers_json->s); |
| 184 | cJSON_Delete(hdrs); |
| 185 | goto error; |
| 186 | } |
| 187 | |
| 188 | int pseudo_headers_done = 0; |
| 189 | |
| 190 | for (it = hdrs->child; it; it = it->next, nh++) { |
| 191 | if (it->type != cJSON_Object) { |
nothing calls this directly
no test coverage detected