<commit_before>var http_code = require("http").STATUS_CODES;
var logging = require("./logging");
var request = require("request");
var config = require("../config");
var skins = require("./skins");
require("./object-patch");

var session_url = "https://sessionserver.mojang.com/session/minecraft/profile/";
var skins_url = "https://skins.minecraft.net/MinecraftSkins/";
var capes_url = "https://skins.minecraft.net/MinecraftCloaks/";
var textures_url = "http://textures.minecraft.net/texture/";
var mojang_urls = [skins_url, capes_url];

var exp = {};

// helper method that calls `get_username_url` or `get_uuid_info` based on the +usedId+
// +userId+ is used for usernames, while +profile+ is used for UUIDs
// callback: error, url, slim
function get_info(rid, userId, profile, type, callback) {
  if (userId.length <= 16) {
    // username
    exp.get_username_url(rid, userId, type, function(err, url) {
      callback(err, url || null, undefined);
    });
  } else {
    exp.get_uuid_info(profile, type, function(url, slim) {
      callback(null, url || null, slim);
    });
  }
}

// performs a GET request to the +url+
// +options+ object includes these options:
//   encoding (string), default is to return a buffer
// callback: the body, response,
// and error buffer. get_from helper method is available
exp.get_from_options = function(rid, url, options, callback) {
  request.get({
    url: url,
    headers: {
      "User-Agent": "Crafatar (+https://crafatar.com)"
    },
    timeout: config.server.http_timeout,
    followRedirect: false,
    encoding: options.encoding || null,
  }, function(error, response, body) {
    // log url + code + description
    var code = response && response.statusCode;

    var logfunc = code && code < 405 ? logging.debug : logging.warn;
    logfunc(rid, url, code || error && error.code, http_code[code]);

    // not necessarily used
    var e = new Error(code);
    e.name = "HTTP";
    e.code = "HTTPERROR";

    switch (code) {
      case 200:
      case 301:
      case 302: // never seen, but mojang might use it in future
      case 307: // never seen, but mojang might use it in future
      case 308: // never seen, but mojang might use it in future
        // these are okay
        break;
      case 204: // no content, used like 404 by mojang. making sure it really has no content
      case 404:
        // can be cached as null
        body = null;
        break;
      case 429: // this shouldn't usually happen, but occasionally does
      case 500:
      case 503:
      case 504:
        // we don't want to cache this
        error = error || e;
        body = null;
        break;
      default:
        if (!error) {
          // Probably 500 or the likes
          logging.error(rid, "Unexpected response:", code, body);
        }
        error = error || e;
        body = null;
        break;
    }

    if (body && !body.length) {
      // empty response
      body = null;
    }

    callback(body, response, error);
  });
};

// helper method for get_from_options, no options required<commit_msg>catch ExtremelyRare™ 502 response from CloudFront<commit_after>var http_code = require("http").STATUS_CODES;
var logging = require("./logging");
var request = require("request");
var config = require("../config");
var skins = require("./skins");
require("./object-patch");

var session_url = "https://sessionserver.mojang.com/session/minecraft/profile/";
var skins_url = "https://skins.minecraft.net/MinecraftSkins/";
var capes_url = "https://skins.minecraft.net/MinecraftCloaks/";
var textures_url = "http://textures.minecraft.net/texture/";
var mojang_urls = [skins_url, capes_url];

var exp = {};

// helper method that calls `get_username_url` or `get_uuid_info` based on the +usedId+
// +userId+ is used for usernames, while +profile+ is used for UUIDs
// callback: error, url, slim
function get_info(rid, userId, profile, type, callback) {
  if (userId.length <= 16) {
    // username
    exp.get_username_url(rid, userId, type, function(err, url) {
      callback(err, url || null, undefined);
    });
  } else {
    exp.get_uuid_info(profile, type, function(url, slim) {
      callback(null, url || null, slim);
    });
  }
}

// performs a GET request to the +url+
// +options+ object includes these options:
//   encoding (string), default is to return a buffer
// callback: the body, response,
// and error buffer. get_from helper method is available
exp.get_from_options = function(rid, url, options, callback) {
  request.get({
    url: url,
    headers: {
      "User-Agent": "Crafatar (+https://crafatar.com)"
    },
    timeout: config.server.http_timeout,
    followRedirect: false,
    encoding: options.encoding || null,
  }, function(error, response, body) {
    // log url + code + description
    var code = response && response.statusCode;

    var logfunc = code && code < 405 ? logging.debug : logging.warn;
    logfunc(rid, url, code || error && error.code, http_code[code]);

    // not necessarily used
    var e = new Error(code);
    e.name = "HTTP";
    e.code = "HTTPERROR";

    switch (code) {
      case 200:
      case 301:
      case 302: // never seen, but mojang might use it in future
      case 307: // never seen, but mojang might use it in future
      case 308: // never seen, but mojang might use it in future
        // these are okay
        break;
      case 204: // no content, used like 404 by mojang. making sure it really has no content
      case 404:
        // can be cached as null
        body = null;
        break;
      case 429: // this shouldn't usually happen, but occasionally does
      case 500:
      case 502: // CloudFront can't reach mojang origin
      case 503:
      case 504:
        // we don't want to cache this
        error = error || e;
        body = null;
        break;
      default:
        if (!error) {
          // Probably 500 or the likes
          logging.error(rid, "Unexpected response:", code, body);
        }
        error = error || e;
        body = null;
        break;
    }

    if (body && !body.length) {
      // empty response
      body = null;
    }

    callback(body, response, error);
  });
};

// helper method for get_from_options, no options required
{'fraction_non_alphanumeric': 0.12816811036252806, 'fraction_numerical': 0.01507860121912095, 'mean_word_length': 2.6334498834498836, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1256427', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1875, 'n_words': 623}