예제 #1
0
static PyObject *
Codec_encode(Codec *self, PyObject *args)
{
  char* in = NULL;
  int inlen = 0;
  PyObject *result = NULL;

  /* Make sure we can handle unicode string inputs.
     The encoded result will always be a plain ascii string.
     FIXME not true -- eg what if safe set has 0xff */

  if (!PyArg_ParseTuple(args, "et#:encode", "utf8", &in, &inlen))
    goto done;

  char* out = NULL;
  Py_ssize_t size;

  /* First pass: calculate size of encoded string.
     Create a new string object of exactly that size. */

  size = percent_encode(in, inlen, NULL, self->chrtohex);

  if (!(result = PyString_FromStringAndSize(NULL,size)))
    goto done;

  /* Second pass: actually encode this time. */

  out = PyString_AsString(result);
  size = percent_encode(in, inlen, out, self->chrtohex);

done:
  if (in) PyMem_Free(in);
  return result;
}
예제 #2
0
Iter decode_encoded_unreserved_chars(Iter first, Iter last) {

  // unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"

  const auto is_unreserved = [](char c)
    {
      return std::isalnum(c, std::locale())
      || '-' == c
      || '.' == c
      || '_' == c
      || '~' == c;
    };

  auto it = first, it2 = first;
  while (it != last) {
    if (*it == '%') {
      const auto sfirst = it;
      const auto opt_char = percent_encode(sfirst);
      if (opt_char && is_unreserved(*opt_char)) {
        *it2 = *opt_char;
        ++it; ++it;
      } else {
        *it2 = *it;
      }
    }
    else {
      *it2 = *it;
    }
    ++it; ++it2;
  }
  return it2;
}
예제 #3
0
static char *
twitter_param(Node *node, TupleDesc tupdesc)
{
	if (node == NULL)
		return NULL;

	if (IsA(node, OpExpr))
	{
		OpExpr	   *op = (OpExpr *) node;
		Node	   *left, *right;
		Index		varattno;
		char	   *key, *val;

		if (list_length(op->args) != 2)
			return NULL;
		left = list_nth(op->args, 0);
		if (!IsA(left, Var))
			return NULL;
		varattno = ((Var *) left)->varattno;
		Assert(0 < varattno && varattno <= tupdesc->natts);
		key = NameStr(tupdesc->attrs[varattno - 1]->attname);

		if (strcmp(key, "q") == 0)
		{
			right = list_nth(op->args, 1);
			if (op->opfuncid != PROCID_TEXTEQ)
				elog(ERROR, "invalid operator");

			if (IsA(right, Const))
			{
				StringInfoData	buf;

				initStringInfo(&buf);
				val = TextDatumGetCString(((Const *) right)->constvalue);
				appendStringInfo(&buf, "q=%s",
					percent_encode((unsigned char *) val, -1));
				return buf.data;
			}
			else
				elog(ERROR, "twitter_fdw: parameter q must be a constant");
		}
	}

	return NULL;
}