2
2
import Service , { inject as service } from '@ember/service' ;
3
3
import { get , set } from '@ember/object' ;
4
4
5
+ import { CACHE_CONTROL , CONTENT_TYPE } from 'consul-ui/utils/http/headers' ;
6
+
7
+ import { HEADERS_TOKEN as CONSUL_TOKEN } from 'consul-ui/utils/http/consul' ;
8
+
5
9
import { env } from 'consul-ui/env' ;
6
10
import getObjectPool from 'consul-ui/utils/get-object-pool' ;
7
11
import Request from 'consul-ui/utils/http/request' ;
@@ -29,7 +33,7 @@ class HTTPError extends Error {
29
33
}
30
34
}
31
35
const dispose = function ( request ) {
32
- if ( request . headers ( ) [ 'content-type' ] === 'text/event-stream' ) {
36
+ if ( request . headers ( ) [ CONTENT_TYPE . toLowerCase ( ) ] === 'text/event-stream' ) {
33
37
const xhr = request . connection ( ) ;
34
38
// unsent and opened get aborted
35
39
// headers and loading means wait for it
@@ -127,30 +131,40 @@ export default Service.extend({
127
131
const [ url , ...headerParts ] = urlParts . join ( ' ' ) . split ( '\n' ) ;
128
132
129
133
return client . settings . findBySlug ( 'token' ) . then ( function ( token ) {
134
+ const requestHeaders = createHeaders ( headerParts ) ;
130
135
const headers = {
131
136
// default to application/json
132
137
...{
133
- 'Content-Type' : 'application/json; charset=utf-8' ,
138
+ [ CONTENT_TYPE ] : 'application/json; charset=utf-8' ,
134
139
} ,
135
140
// add any application level headers
136
141
...{
137
- 'X-Consul-Token' : typeof token . SecretID === 'undefined' ? '' : token . SecretID ,
142
+ [ CONSUL_TOKEN ] : typeof token . SecretID === 'undefined' ? '' : token . SecretID ,
138
143
} ,
139
144
// but overwrite or add to those from anything in the specific request
140
- ...createHeaders ( headerParts ) ,
145
+ ...requestHeaders ,
141
146
} ;
147
+ // We use cache-control in the response
148
+ // but we don't want to send it, but we artificially
149
+ // tag it onto the response below if it is set on the request
150
+ delete headers [ CACHE_CONTROL ] ;
142
151
143
152
return new Promise ( function ( resolve , reject ) {
144
153
const options = {
145
154
url : url . trim ( ) ,
146
155
method : method ,
147
- contentType : headers [ 'Content-Type' ] ,
156
+ contentType : headers [ CONTENT_TYPE ] ,
148
157
// type: 'json',
149
158
complete : function ( xhr , textStatus ) {
150
159
client . complete ( this . id ) ;
151
160
} ,
152
161
success : function ( response , status , xhr ) {
153
162
const headers = createHeaders ( xhr . getAllResponseHeaders ( ) . split ( '\n' ) ) ;
163
+ if ( typeof requestHeaders [ CACHE_CONTROL ] !== 'undefined' ) {
164
+ // if cache-control was on the request, artificially tag
165
+ // it back onto the response, also see comment above
166
+ headers [ CACHE_CONTROL ] = requestHeaders [ CACHE_CONTROL ] ;
167
+ }
154
168
const respond = function ( cb ) {
155
169
return cb ( headers , response ) ;
156
170
} ;
@@ -191,7 +205,7 @@ export default Service.extend({
191
205
// for write-like actions
192
206
// potentially we should change things so you _have_ to do that
193
207
// as doing it this way is a little magical
194
- if ( method !== 'GET' && headers [ 'Content-Type' ] . indexOf ( 'json' ) !== - 1 ) {
208
+ if ( method !== 'GET' && headers [ CONTENT_TYPE ] . indexOf ( 'json' ) !== - 1 ) {
195
209
options . data = JSON . stringify ( body ) ;
196
210
} else {
197
211
// TODO: Does this need urlencoding? Assuming jQuery does this
@@ -204,7 +218,7 @@ export default Service.extend({
204
218
// also see adapters/kv content-types in requestForCreate/UpdateRecord
205
219
// also see https://github.com/hashicorp/consul/issues/3804
206
220
options . contentType = 'application/json; charset=utf-8' ;
207
- headers [ 'Content-Type' ] = options . contentType ;
221
+ headers [ CONTENT_TYPE ] = options . contentType ;
208
222
//
209
223
options . beforeSend = function ( xhr ) {
210
224
if ( headers ) {
0 commit comments