//
//  NSURLRequest+ZYJQNRequest.m
//  AppTest
//
//  Created by yangsen on 2020/4/8.
//  Copyright © 2020 com.qiniu. All rights reserved.
//

#import <objc/runtime.h>
#import "NSURLRequest+ZYJQNRequest.h"


@implementation NSURLRequest(ZYJQNRequest)

#define kZYJQNURLReuestHostKey @"Host"
#define kZYJQNURLReuestIdentifierKey @"ZYJQNURLReuestIdentifier"
- (BOOL)ZYJQN_isQiNiuRequest{
    if (self.ZYJQN_identifier && self.ZYJQN_domain) {
        return YES;
    } else {
        return NO;
    }
}

- (NSString *)ZYJQN_identifier{
    return self.allHTTPHeaderFields[kZYJQNURLReuestIdentifierKey];
}

- (NSString *)ZYJQN_domain{
    NSString *host = self.allHTTPHeaderFields[kZYJQNURLReuestHostKey];
    if (host == nil) {
        host = self.URL.host;
    }
    return host;
}

- (NSDictionary *)ZYJQN_allHTTPHeaderFields{
    NSDictionary *headerFields = [self.allHTTPHeaderFields copy];
    NSMutableDictionary *headerFieldsNew = [NSMutableDictionary dictionary];
    for (NSString *key in headerFields) {
        if (![key isEqualToString:kZYJQNURLReuestIdentifierKey]) {
            [headerFieldsNew setObject:headerFields[key] forKey:key];
        }
    }
    return [headerFieldsNew copy];
}

+ (instancetype)ZYJQN_requestWithURL:(NSURL *)url{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setValue:url.host forHTTPHeaderField:kZYJQNURLReuestHostKey];
    return request;
}


- (NSData *)ZYJQN_getHttpBody{
    
    if (self.HTTPBody || ![self.HTTPMethod isEqualToString:@"POST"]) {
        return self.HTTPBody;
    }
    
    NSInteger maxLength = 1024;
    uint8_t d[maxLength];
    
    NSInputStream *stream = self.HTTPBodyStream;
    NSMutableData *data = [NSMutableData data];
    
    [stream open];
    
    BOOL end = NO;
    
    while (!end) {
        NSInteger bytesRead = [stream read:d maxLength:maxLength];
        if (bytesRead == 0) {
            end = YES;
        } else if (bytesRead == -1){
            end = YES;
        } else if (stream.streamError == nil){
            [data appendBytes:(void *)d length:bytesRead];
       }
    }
    [stream close];
    return [data copy];
}

- (BOOL)ZYJQN_isHttps{
    if ([self.URL.absoluteString rangeOfString:@"https://"].location != NSNotFound) {
        return YES;
    } else {
        return NO;
    }
}
@end


@implementation NSMutableURLRequest(ZYJQNRequest)

- (void)setZYJQN_domain:(NSString *)ZYJQN_domain{
    if (ZYJQN_domain) {
        [self addValue:ZYJQN_domain forHTTPHeaderField:kZYJQNURLReuestHostKey];
    } else {
        [self setValue:nil forHTTPHeaderField:kZYJQNURLReuestHostKey];
    }

    NSString *identifier = [NSString stringWithFormat:@"%p-%@", &self, ZYJQN_domain];
    [self setZYJQN_identifier:identifier];
}

- (void)setZYJQN_identifier:(NSString *)ZYJQN_identifier{
    if (ZYJQN_identifier) {
        [self addValue:ZYJQN_identifier forHTTPHeaderField:kZYJQNURLReuestIdentifierKey];
    } else {
        [self setValue:nil forHTTPHeaderField:kZYJQNURLReuestIdentifierKey];
    }
}

@end
