[pycharm] DataView/PlotsView (fix): Added getMaxRequestsPerMinute in RestService and overridden in PySciRestService to prevent 429 HHTP error. #PY-43687 Fixed

GitOrigin-RevId: 4e61e154062af8dcf0247988080645bf028ea5ee
This commit is contained in:
Nikita Pavlenko
2024-05-17 16:50:51 +02:00
committed by intellij-monorepo-bot
parent 62f90e15eb
commit 18fb57465c
2 changed files with 12 additions and 11 deletions

View File

@@ -224,6 +224,7 @@ a:org.jetbrains.ide.RestService
- pf:getGson():com.google.gson.Gson
- sf:getIntParameter(java.lang.String,io.netty.handler.codec.http.QueryStringDecoder):I
- sf:getLastFocusedOrOpenedProject():com.intellij.openapi.project.Project
- p:getMaxRequestsPerMinute():I
- p:getReportErrorsAsPlainText():Z
- p:getRequesterId(io.netty.handler.codec.http.QueryStringDecoder,io.netty.handler.codec.http.FullHttpRequest,io.netty.channel.ChannelHandlerContext):java.lang.Object
- pa:getServiceName():java.lang.String

View File

@@ -98,15 +98,7 @@ abstract class RestService : HttpRequestHandler() {
@JvmStatic
fun sendStatus(status: HttpResponseStatus, keepAlive: Boolean, channel: Channel) {
val response = DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status)
HttpUtil.setContentLength(response, 0)
response.addCommonHeaders()
response.addNoCache()
if (keepAlive) {
HttpUtil.setKeepAlive(response, true)
}
response.headers().set("X-Frame-Options", "Deny")
response.send(channel, !keepAlive)
responseStatus(status, keepAlive, channel)
}
@JvmStatic
@@ -214,6 +206,14 @@ abstract class RestService : HttpRequestHandler() {
return method === HttpMethod.GET
}
/**
* If the requests per minute counter exceeds this value, the exception [HttpResponseStatus.TOO_MANY_REQUESTS] will be sent.
* @return The value of "ide.rest.api.requests.per.minute" Registry key or '30', if the key does not exist.
*/
protected open fun getMaxRequestsPerMinute(): Int {
return Registry.intValue("ide.rest.api.requests.per.minute", 30)
}
override fun process(urlDecoder: QueryStringDecoder, request: FullHttpRequest, context: ChannelHandlerContext): Boolean {
try {
if (!isHostTrusted(request, urlDecoder)) {
@@ -222,7 +222,7 @@ abstract class RestService : HttpRequestHandler() {
}
val counter = abuseCounter.get(getRequesterId(urlDecoder, request, context))!!
if (counter.incrementAndGet() > Registry.intValue("ide.rest.api.requests.per.minute", 30)) {
if (counter.incrementAndGet() > getMaxRequestsPerMinute()) {
HttpResponseStatus.TOO_MANY_REQUESTS.sendError(context.channel(), request)
return true
}
@@ -371,4 +371,4 @@ fun HttpResponseStatus.orInSafeMode(safeStatus: HttpResponseStatus): HttpRespons
else {
return safeStatus
}
}
}