KeyValueRanges paramsKV = helper.params.reset(); KeyValueRanges headersKV = helper.headersKV.reset(); KeyValueRanges cookiesKV = helper.cookies.reset(); Map<String, String> params = U.cast(paramsKV.toMap(buf, true, true, false)); Map<String, String> headers = U.cast(headersKV.toMap(buf, false, false, true)); Map<String, String> cookies = U.cast(cookiesKV.toMap(buf, false, false, false)); KeyValueRanges postedKV = helper.pairs3.reset();
@Override public Data get_(String name) { Buf buf = src.buffer(); BufRange range = ranges.get(buf, name.getBytes(), false); return range != null ? new DecodedData(src, range) : null; }
@SuppressWarnings("unchecked") public void toUrlDecodedParams(Buf src, Map<String, Object> params, BufRanges contentTypes) { for (int i = 0; i < count; i++) { String key = keys[i].str(src.bytes()); String val = values[i].str(src.bytes()); boolean isJSON = isJSON(src, contentTypes, i); key = URIs.urlDecodeOrKeepOriginal(key); if (!isJSON) { val = URIs.urlDecodeOrKeepOriginal(val); if (key.endsWith("[]")) { key = Str.sub(key, 0, -2); List<String> list = (List<String>) params.get(key); if (list == null) { list = U.list(); params.put(key, list); } list.add(val); } else { params.put(key, val); } } else { params.put(key, JSON.parse(val)); } } }
private void parseMultiPart(Buf src, KeyValueRanges data, BufRanges dataContentTypes, Map<String, List<Upload>> files, RapidoidHelper helper, int from, int to) { KeyValueRanges headers = helper.headersKV.reset(); BufRange partBody = helper.ranges4.ranges[0]; BufRange contType = helper.ranges4.ranges[1]; BufRange disposition = headers.get(src, CONTENT_DISPOSITION, false); BufRange contentType = headers.get(src, CONTENT_TYPE, false); BufRange encoding = headers.get(src, CONTENT_TRANSFER_ENCODING, false); int ind = data.add(); data.keys[ind].assign(name); data.values[ind].assign(partBody);
@Override public synchronized Map<String, String> get() { if (values == null) { values = ranges.toMap(src.buffer(), true, true, false); } return values; }
public void parseHeadersIntoKV(Buf buf, BufRanges headers, KeyValueRanges headersKV, KeyValueRanges cookies, RapidoidHelper helper) { BufRange cookie = helper.ranges5.ranges[0]; for (int i = 0; i < headers.count; i++) { BufRange hdr = headers.ranges[i]; int ind = headersKV.add(); BufRange key = headersKV.keys[ind]; BufRange val = headersKV.values[ind]; assert !hdr.isEmpty(); boolean split = BytesUtil.split(buf.bytes(), hdr, COL, key, val, true); U.must(split, "Invalid HTTP header!"); if (cookies != null && BytesUtil.matches(buf.bytes(), key, COOKIE, false)) { headersKV.count--; // don't include cookies in headers do { BytesUtil.split(buf.bytes(), val, SEMI_COL, cookie, val, true); int cind = cookies.add(); BytesUtil.split(buf.bytes(), cookie, EQ, cookies.keys[cind], cookies.values[cind], true); } while (!val.isEmpty()); } } }
public int add() { if (count >= max()) { throw U.rte("too many key-values!"); } return count++; }
@SuppressWarnings("unchecked") public boolean parsePosted(Buf input, KeyValueRanges headersKV, BufRange rBody, KeyValueRanges posted, Map<String, List<Upload>> files, RapidoidHelper helper, Map<String, Object> dest) { BufRanges dataContentTypes = helper.ranges3.reset(); boolean completed = parseBody(input, headersKV, rBody, posted, dataContentTypes, files, helper); posted.toUrlDecodedParams(input, dest, dataContentTypes); return completed; }
private void parseMultiPart(Buf src, KeyValueRanges data, BufRanges dataContentTypes, Map<String, List<Upload>> files, RapidoidHelper helper, int from, int to) { KeyValueRanges headers = helper.headersKV.reset(); BufRange partBody = helper.ranges4.ranges[0]; BufRange contType = helper.ranges4.ranges[1]; BufRange disposition = headers.get(src, CONTENT_DISPOSITION, false); BufRange contentType = headers.get(src, CONTENT_TYPE, false); BufRange encoding = headers.get(src, CONTENT_TRANSFER_ENCODING, false); int ind = data.add(); data.keys[ind].assign(name); data.values[ind].assign(partBody);
public Map<String, String> toMap(Buf src, boolean urlDecodeKeys, boolean urlDecodeVals, boolean lowerCaseKeys) { Map<String, String> map = U.map(); toMap(src, urlDecodeKeys, urlDecodeVals, lowerCaseKeys, map); return map; }
private void parseURLEncodedKV(Buf buf, KeyValueRanges params, BufRange body) { int pos = buf.position(); int limit = buf.limit(); buf.position(body.start); buf.limit(body.limit()); while (buf.hasRemaining()) { int ind = params.add(); int which = buf.scanTo(EQ, AMP, params.keys[ind], false); if (which == 1) { buf.scanTo(AMP, params.values[ind], false); } } buf.position(pos); buf.limit(limit); }
public int add() { if (count >= max()) { throw U.rte("too many key-values!"); } return count++; }
@SuppressWarnings("unchecked") public boolean parsePosted(Buf input, KeyValueRanges headersKV, BufRange rBody, KeyValueRanges posted, Map<String, List<Upload>> files, RapidoidHelper helper, Map<String, Object> dest) { BufRanges dataContentTypes = helper.ranges3.reset(); boolean completed = parseBody(input, headersKV, rBody, posted, dataContentTypes, files, helper); posted.toUrlDecodedParams(input, dest, dataContentTypes); return completed; }
KeyValueRanges paramsKV = helper.params.reset(); KeyValueRanges headersKV = helper.headersKV.reset(); KeyValueRanges cookiesKV = helper.cookies.reset(); Map<String, String> params = U.cast(paramsKV.toMap(buf, true, true, false)); Map<String, String> headers = U.cast(headersKV.toMap(buf, false, false, true)); Map<String, String> cookies = U.cast(cookiesKV.toMap(buf, false, false, false)); KeyValueRanges postedKV = helper.pairs3.reset();
public Map<String, String> toMap(Buf src, boolean urlDecodeKeys, boolean urlDecodeVals, boolean lowerCaseKeys) { Map<String, String> map = U.map(); toMap(src, urlDecodeKeys, urlDecodeVals, lowerCaseKeys, map); return map; }
private HttpContentType getContentType(Buf buf, KeyValueRanges headers, BufRange multipartBoundary) { BufRange contType = headers.get(buf, CONTENT_TYPE, false); if (contType != null) { if (BytesUtil.startsWith(buf.bytes(), contType, CT_FORM_URLENCODED, false)) { multipartBoundary.reset(); return HttpContentType.FORM_URLENCODED; } if (BytesUtil.startsWith(buf.bytes(), contType, CT_JSON, false)) { multipartBoundary.reset(); return HttpContentType.JSON; } if (BytesUtil.startsWith(buf.bytes(), contType, CT_MULTIPART_FORM_DATA_BOUNDARY1, false)) { multipartBoundary.setInterval(contType.start + CT_MULTIPART_FORM_DATA_BOUNDARY1.length, contType.limit()); return HttpContentType.MULTIPART; } if (BytesUtil.startsWith(buf.bytes(), contType, CT_MULTIPART_FORM_DATA_BOUNDARY2, false)) { multipartBoundary.setInterval(contType.start + CT_MULTIPART_FORM_DATA_BOUNDARY2.length, contType.limit()); return HttpContentType.MULTIPART; } if (BytesUtil.startsWith(buf.bytes(), contType, CT_MULTIPART_FORM_DATA, false)) { multipartBoundary.reset(); return HttpContentType.MULTIPART; } } multipartBoundary.reset(); return contType != null ? HttpContentType.OTHER : HttpContentType.NOT_FOUND; }
public void parseHeadersIntoKV(Buf buf, BufRanges headers, KeyValueRanges headersKV, KeyValueRanges cookies, RapidoidHelper helper) { BufRange cookie = helper.ranges5.ranges[0]; for (int i = 0; i < headers.count; i++) { BufRange hdr = headers.ranges[i]; int ind = headersKV.add(); BufRange key = headersKV.keys[ind]; BufRange val = headersKV.values[ind]; assert !hdr.isEmpty(); boolean split = BytesUtil.split(buf.bytes(), hdr, COL, key, val, true); U.must(split, "Invalid HTTP header!"); if (cookies != null && BytesUtil.matches(buf.bytes(), key, COOKIE, false)) { headersKV.count--; // don't include cookies in headers do { BytesUtil.split(buf.bytes(), val, SEMI_COL, cookie, val, true); int cind = cookies.add(); BytesUtil.split(buf.bytes(), cookie, EQ, cookies.keys[cind], cookies.values[cind], true); } while (!val.isEmpty()); } } }
@SuppressWarnings("unchecked") public void toUrlDecodedParams(Buf src, Map<String, Object> params, BufRanges contentTypes) { for (int i = 0; i < count; i++) { String key = keys[i].str(src.bytes()); String val = values[i].str(src.bytes()); boolean isJSON = isJSON(src, contentTypes, i); key = Msc.urlDecodeOrKeepOriginal(key); if (!isJSON) { val = Msc.urlDecodeOrKeepOriginal(val); if (key.endsWith("[]")) { key = Str.sub(key, 0, -2); List<String> list = (List<String>) params.get(key); if (list == null) { list = U.list(); params.put(key, list); } list.add(val); } else { params.put(key, val); } } else { params.put(key, JSON.parse(val)); } } }
private HttpContentType getContentType(Buf buf, KeyValueRanges headers, BufRange multipartBoundary) { BufRange contType = headers.get(buf, CONTENT_TYPE, false); if (contType != null) { if (BytesUtil.startsWith(buf.bytes(), contType, CT_FORM_URLENCODED, false)) { multipartBoundary.reset(); return HttpContentType.FORM_URLENCODED; } if (BytesUtil.startsWith(buf.bytes(), contType, CT_JSON, false)) { multipartBoundary.reset(); return HttpContentType.JSON; } if (BytesUtil.startsWith(buf.bytes(), contType, CT_MULTIPART_FORM_DATA_BOUNDARY1, false)) { multipartBoundary.setInterval(contType.start + CT_MULTIPART_FORM_DATA_BOUNDARY1.length, contType.limit()); return HttpContentType.MULTIPART; } if (BytesUtil.startsWith(buf.bytes(), contType, CT_MULTIPART_FORM_DATA_BOUNDARY2, false)) { multipartBoundary.setInterval(contType.start + CT_MULTIPART_FORM_DATA_BOUNDARY2.length, contType.limit()); return HttpContentType.MULTIPART; } if (BytesUtil.startsWith(buf.bytes(), contType, CT_MULTIPART_FORM_DATA, false)) { multipartBoundary.reset(); return HttpContentType.MULTIPART; } } multipartBoundary.reset(); return contType != null ? HttpContentType.OTHER : HttpContentType.NOT_FOUND; }