Skip to content

Commit fe7b260

Browse files
committed
graph, server: use constants for header names
1 parent 0f33f0f commit fe7b260

File tree

5 files changed

+41
-27
lines changed

5 files changed

+41
-27
lines changed

graph/src/data/query/result.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ use crate::{
33
data::graphql::SerializableValue,
44
prelude::{q, CacheWeight, SubgraphDeploymentId},
55
};
6+
use http::header::{
7+
ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN,
8+
CONTENT_TYPE,
9+
};
610
use serde::ser::*;
711
use serde::Serialize;
812
use std::collections::BTreeMap;
@@ -158,10 +162,10 @@ impl QueryResults {
158162
serde_json::to_string(self).expect("Failed to serialize GraphQL response to JSON");
159163
http::Response::builder()
160164
.status(status_code)
161-
.header("Access-Control-Allow-Origin", "*")
162-
.header("Access-Control-Allow-Headers", "Content-Type, User-Agent")
163-
.header("Access-Control-Allow-Methods", "GET, OPTIONS, POST")
164-
.header("Content-Type", "application/json")
165+
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
166+
.header(ACCESS_CONTROL_ALLOW_HEADERS, "Content-Type, User-Agent")
167+
.header(ACCESS_CONTROL_ALLOW_METHODS, "GET, OPTIONS, POST")
168+
.header(CONTENT_TYPE, "application/json")
165169
.body(T::from(json))
166170
.unwrap()
167171
}

graph/src/log/elastic.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::time::Duration;
77

88
use chrono::prelude::{SecondsFormat, Utc};
99
use futures03::TryFutureExt;
10+
use http::header::CONTENT_TYPE;
1011
use reqwest;
1112
use reqwest::Client;
1213
use serde::ser::Serializer as SerdeSerializer;
@@ -265,11 +266,11 @@ impl ElasticDrain {
265266
let header = match config.general.username {
266267
Some(username) => client
267268
.post(batch_url)
268-
.header("Content-Type", "application/json")
269+
.header(CONTENT_TYPE, "application/json")
269270
.basic_auth(username, config.general.password.clone()),
270271
None => client
271272
.post(batch_url)
272-
.header("Content-Type", "application/json"),
273+
.header(CONTENT_TYPE, "application/json"),
273274
};
274275
header
275276
.body(batch_body)

server/http/src/service.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use std::time::Instant;
88
use graph::prelude::*;
99
use graph::{components::server::query::GraphQLServerError, data::query::QueryTarget};
1010
use http::header;
11-
use http::header::{ACCESS_CONTROL_ALLOW_ORIGIN, LOCATION};
11+
use http::header::{
12+
ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN,
13+
CONTENT_TYPE, LOCATION,
14+
};
1215
use hyper::service::Service;
1316
use hyper::{Body, Method, Request, Response, StatusCode};
1417

@@ -217,9 +220,9 @@ where
217220
async {
218221
Ok(Response::builder()
219222
.status(200)
220-
.header("Access-Control-Allow-Origin", "*")
221-
.header("Access-Control-Allow-Headers", "Content-Type, User-Agent")
222-
.header("Access-Control-Allow-Methods", "GET, OPTIONS, POST")
223+
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
224+
.header(ACCESS_CONTROL_ALLOW_HEADERS, "Content-Type, User-Agent")
225+
.header(ACCESS_CONTROL_ALLOW_METHODS, "GET, OPTIONS, POST")
223226
.body(Body::from(""))
224227
.unwrap())
225228
}
@@ -343,7 +346,7 @@ where
343346
Ok(response) => Ok(response),
344347
Err(err @ GraphQLServerError::ClientError(_)) => Ok(Response::builder()
345348
.status(400)
346-
.header("Content-Type", "text/plain")
349+
.header(CONTENT_TYPE, "text/plain")
347350
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
348351
.body(Body::from(err.to_string()))
349352
.unwrap()),
@@ -352,7 +355,7 @@ where
352355

353356
Ok(Response::builder()
354357
.status(400)
355-
.header("Content-Type", "text/plain")
358+
.header(CONTENT_TYPE, "text/plain")
356359
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
357360
.body(Body::from(format!("Query error: {}", err)))
358361
.unwrap())
@@ -362,7 +365,7 @@ where
362365

363366
Ok(Response::builder()
364367
.status(500)
365-
.header("Content-Type", "text/plain")
368+
.header(CONTENT_TYPE, "text/plain")
366369
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
367370
.body(Body::from(format!("Internal server error: {}", err)))
368371
.unwrap())

server/index-node/src/explorer.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
//! in this file is private API and experimental and subject to change at
33
//! any time
44
use http::{Response, StatusCode};
5-
use hyper::header::ACCESS_CONTROL_ALLOW_ORIGIN;
5+
use hyper::header::{
6+
ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN,
7+
CONTENT_TYPE,
8+
};
69
use hyper::Body;
710
use std::{
811
collections::HashMap,
@@ -234,7 +237,7 @@ where
234237
fn handle_not_found() -> Result<Response<Body>, GraphQLServerError> {
235238
Ok(Response::builder()
236239
.status(StatusCode::NOT_FOUND)
237-
.header("Content-Type", "text/plain")
240+
.header(CONTENT_TYPE, "text/plain")
238241
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
239242
.body(Body::from("Not found\n"))
240243
.unwrap())
@@ -246,10 +249,10 @@ fn as_http_response(value: &q::Value) -> http::Response<Body> {
246249
.expect("Failed to serialize response to JSON");
247250
http::Response::builder()
248251
.status(status_code)
249-
.header("Access-Control-Allow-Origin", "*")
250-
.header("Access-Control-Allow-Headers", "Content-Type, User-Agent")
251-
.header("Access-Control-Allow-Methods", "GET, OPTIONS, POST")
252-
.header("Content-Type", "application/json")
252+
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
253+
.header(ACCESS_CONTROL_ALLOW_HEADERS, "Content-Type, User-Agent")
254+
.header(ACCESS_CONTROL_ALLOW_METHODS, "GET, OPTIONS, POST")
255+
.header(CONTENT_TYPE, "application/json")
253256
.body(Body::from(json))
254257
.unwrap()
255258
}

server/index-node/src/service.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use http::header::{self, ACCESS_CONTROL_ALLOW_ORIGIN, LOCATION};
1+
use http::header::{
2+
self, ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN,
3+
CONTENT_TYPE, LOCATION,
4+
};
25
use hyper::service::Service;
36
use hyper::{Body, Method, Request, Response, StatusCode};
47
use std::task::Context;
@@ -129,9 +132,9 @@ where
129132
fn handle_graphql_options(_request: Request<Body>) -> Response<Body> {
130133
Response::builder()
131134
.status(200)
132-
.header("Access-Control-Allow-Origin", "*")
133-
.header("Access-Control-Allow-Headers", "Content-Type, User-Agent")
134-
.header("Access-Control-Allow-Methods", "GET, OPTIONS, POST")
135+
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
136+
.header(ACCESS_CONTROL_ALLOW_HEADERS, "Content-Type, User-Agent")
137+
.header(ACCESS_CONTROL_ALLOW_METHODS, "GET, OPTIONS, POST")
135138
.body(Body::from(""))
136139
.unwrap()
137140
}
@@ -157,7 +160,7 @@ where
157160
Response::builder()
158161
.status(StatusCode::NOT_FOUND)
159162
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
160-
.header("Content-Type", "text/plain")
163+
.header(CONTENT_TYPE, "text/plain")
161164
.body(Body::from("Not found\n"))
162165
.unwrap()
163166
}
@@ -228,7 +231,7 @@ where
228231

229232
Ok(Response::builder()
230233
.status(400)
231-
.header("Content-Type", "text/plain")
234+
.header(CONTENT_TYPE, "text/plain")
232235
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
233236
.body(Body::from(format!("Invalid request: {}", err)))
234237
.unwrap())
@@ -238,7 +241,7 @@ where
238241

239242
Ok(Response::builder()
240243
.status(400)
241-
.header("Content-Type", "text/plain")
244+
.header(CONTENT_TYPE, "text/plain")
242245
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
243246
.body(Body::from(format!("Query error: {}", err)))
244247
.unwrap())
@@ -248,7 +251,7 @@ where
248251

249252
Ok(Response::builder()
250253
.status(500)
251-
.header("Content-Type", "text/plain")
254+
.header(CONTENT_TYPE, "text/plain")
252255
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
253256
.body(Body::from(format!("Internal server error: {}", err)))
254257
.unwrap())

0 commit comments

Comments
 (0)