0

我正在尝试使用从 nif 返回的数据创建对提供者的 OCSP 请求,我想以这种方式发出请求:

HTTPoison.post!(
    oscp_info[:access],
    :unicode.characters_to_binary(oscp_info[:data], utf8),
    [{"Content-Type", "application/ocsp-request"}],
    timeout: 1000
  )

我在 C 中有一段代码,可以形成从 c 到 erlang 的数据:

static ERL_NIF_TERM CreateElixirBinary(ErlNifEnv *env, const char *str, int strLength)
{
  ERL_NIF_TERM databytes = enif_make_list(env, 0);

  for (int i=0; i < strLength; i++)
  {
    databytes = enif_make_list_cell(env, enif_make_uint(env, str[i]), databytes);
  }

  return databytes;
}

并在 C 中形成映射密钥对

enif_make_map_put(env, osp, enif_make_atom(env, "data"), CreateElixirBinary(env, baseValidationResult.certsCheckInfo[i].data, baseValidationResult.certsCheckInfo[i].dataLen),  &osp);

我有地图 oscp_info

`oscp_info = %{
  access: "http://acsk.any.provider.ua/services/ocsp/",
  crl: "http://acsk.any.provider.ua/crl/PB-S9.crl",
  data: [0, 113, 4294967247, 69, 0, 34, 4294967231, 4294967213, 0, 0, 0, 4,
   4294967272, 4294967169, 4294967187, 4294967227, 4294967201, 4294967277,
   4294967172, 13, 20, 2, 87, 85, 24, 93, 4294967256, 87, 17, 55, 4294967224, 1,
   74, 4294967238, 4294967172, 4294967239, 4294967256, 4294967236, 63,
   4294967173, 4294967186, 4294967212, 4294967208, 4294967184, 17, 4294967235,
   4294967272, ...],
  delta_crl: "http://acsk.any.provider.ua/crldelta/PB-Delta-S9.crl"
}`

如果我使用enif_make_int而不是enif_make_uint数据看起来像:

data: [0, 113, -49, 69, 0, 34, -65, -83, 0, 0, 0, 4, -24, -127, -109, -69,
   -95, -19, -124, 13, 20, 2, 87, 85, 24, 93, -40, 87, 17, 55, -72, 1, 74, -58,
   -124, -57, -40, -60, 63, -123, -110, -84, -88, -112, 17, -61, -24, ...]

问题是数据既不能转换为Unicode也不能转换为二进制,所以我不能发出http请求我应该如何从nif返回数据(char *)才能发出请求?

4

1 回答 1

3

从 c 形成 erlang 二进制文件的正确方法:

ErlNifBinary derDataBin;

enif_alloc_binary(baseValidationResult.certsCheckInfo[i].dataLen, &derDataBin);

memcpy(derDataBin.data, baseValidationResult.certsCheckInfo[i].data, 

baseValidationResult.certsCheckInfo[i].dataLen);

ERL_NIF_TERM derData = enif_make_binary(env, &derDataBin);
于 2018-07-25T10:22:19.507 回答