| 1336 | } |
| 1337 | |
| 1338 | static void SendUnauthorizedResponse(const std::string& user_error_text, Socket* socket, const InputMessageBase* msg) { |
| 1339 | HttpContext* http_request = (HttpContext*)msg; |
| 1340 | const bool is_http2 = http_request->header().is_http2(); |
| 1341 | if (is_http2) { |
| 1342 | // for grpc client |
| 1343 | const H2StreamContext* h2_sctx = static_cast<const H2StreamContext*>(msg); |
| 1344 | brpc::Controller cntl; |
| 1345 | cntl.http_response().set_status_code(200); |
| 1346 | cntl.http_response().set_content_type("application/grpc"); |
| 1347 | cntl.SetFailed(ERPCAUTH, "%s", user_error_text.empty() ? "Fail to authenticate" : user_error_text.c_str()); |
| 1348 | |
| 1349 | SocketMessagePtr<H2UnsentResponse> h2_response( |
| 1350 | H2UnsentResponse::New(&cntl, h2_sctx->stream_id(), true)); |
| 1351 | brpc::Socket::WriteOptions opt; |
| 1352 | opt.ignore_eovercrowded = true; |
| 1353 | socket->Write(h2_response, &opt); |
| 1354 | return; |
| 1355 | } |
| 1356 | |
| 1357 | // Send 403(forbidden) to client. |
| 1358 | HttpHeader header; |
| 1359 | header.set_status_code(HTTP_STATUS_FORBIDDEN); |
| 1360 | butil::IOBuf content; |
| 1361 | content.append(butil::string_printf("[%d]", ERPCAUTH)); |
| 1362 | content.append("Fail to authenticate"); |
| 1363 | if (!user_error_text.empty()) { |
| 1364 | content.append(": "); |
| 1365 | content.append(user_error_text); |
| 1366 | } |
| 1367 | butil::IOBuf res_buf; |
| 1368 | MakeRawHttpResponse(&res_buf, &header, &content); |
| 1369 | Socket::WriteOptions opt; |
| 1370 | opt.ignore_eovercrowded = true; |
| 1371 | if (socket->Write(&res_buf, &opt) != 0) { |
| 1372 | PLOG_IF(WARNING, errno != EPIPE) << "Fail to write into " << *socket; |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | bool VerifyHttpRequest(const InputMessageBase* msg) { |
| 1377 | Server* server = (Server*)msg->arg(); |
no test coverage detected