org.apache.commons.httpclient.util.EncodingUtil Java Examples

The following examples show how to use org.apache.commons.httpclient.util.EncodingUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NTLMv2Scheme.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Processes the NTLM challenge.
 *
 * @param challenge the challenge string
 * @throws MalformedChallengeException is thrown if the authentication challenge
 *                                     is malformed
 */
public void processChallenge(final String challenge) throws MalformedChallengeException {
    String authScheme = AuthChallengeParser.extractScheme(challenge);
    if (!authScheme.equalsIgnoreCase(getSchemeName())) {
        throw new MalformedChallengeException("Invalid NTLM challenge: " + challenge);
    }
    int spaceIndex = challenge.indexOf(' ');
    if (spaceIndex != -1) {
        try {
            type2Message = new Type2Message(Base64.decodeBase64(EncodingUtil.getBytes(
                    challenge.substring(spaceIndex).trim(), "ASCII")));
        } catch (IOException e) {
            throw new MalformedChallengeException("Invalid NTLM challenge: " + challenge, e);
        }
        this.state = TYPE2_MSG_RECEIVED;
    } else {
        this.type2Message = null;
        if (this.state == UNINITIATED) {
            this.state = INITIATED;
        } else {
            this.state = FAILED;
        }
    }
}
 
Example #2
Source File: LaxHttpParser.java    From webarchive-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Read up to <tt>"\n"</tt> from an (unchunked) input stream.
 * If the stream ends before the line terminator is found,
 * the last part of the string will still be returned.
 * If no input data available, <code>null</code> is returned.
 *
 * @param inputStream the stream to read from
 * @param charset charset of HTTP protocol elements
 *
 * @throws IOException if an I/O problem occurs
 * @return a line from the stream
 * 
 * @since 3.0
 */
public static String readLine(InputStream inputStream, String charset) throws IOException {
    LOG.trace("enter LaxHttpParser.readLine(InputStream, String)");
    byte[] rawdata = readRawLine(inputStream);
    if (rawdata == null) {
        return null;
    }
    // strip CR and LF from the end
    int len = rawdata.length;
    int offset = 0;
    if (len > 0) {
        if (rawdata[len - 1] == '\n') {
            offset++;
            if (len > 1) {
                if (rawdata[len - 2] == '\r') {
                    offset++;
                }
            }
        }
    }
    return EncodingUtil.getString(rawdata, 0, len - offset, charset);
}
 
Example #3
Source File: HttpResponse.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
public String getResponse()
{
	if (responseBytes != null)
	{
		if (method instanceof HttpMethodBase)
		{
			// mimic method.getResponseBodyAsString
			return EncodingUtil.getString(responseBytes, ((HttpMethodBase)method).getResponseCharSet());
		}
		else
		{
			return new String(responseBytes);
		}
	}
	else
	{
		return null;
	}
}
 
Example #4
Source File: BasicScheme.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns a basic <tt>Authorization</tt> header value for the given 
 * {@link UsernamePasswordCredentials} and charset.
 * 
 * @param credentials The credentials to encode.
 * @param charset The charset to use for encoding the credentials
 * 
 * @return a basic authorization string
 * 
 * @since 3.0
 */
public static String authenticate(UsernamePasswordCredentials credentials, String charset) {

    LOG.trace("enter BasicScheme.authenticate(UsernamePasswordCredentials, String)");

    if (credentials == null) {
        throw new IllegalArgumentException("Credentials may not be null"); 
    }
    if (charset == null || charset.length() == 0) {
        throw new IllegalArgumentException("charset may not be null or empty");
    }
    StringBuffer buffer = new StringBuffer();
    buffer.append(credentials.getUserName());
    buffer.append(":");
    buffer.append(credentials.getPassword());
    
    return "Basic " + EncodingUtil.getAsciiString(
            Base64.encodeBase64(EncodingUtil.getBytes(buffer.toString(), charset)));
}
 
Example #5
Source File: DigestScheme.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates a random cnonce value based on the current time.
 * 
 * @return The cnonce value as String.
 * @throws HttpClientError if MD5 algorithm is not supported.
 */
public static String createCnonce() {
    LOG.trace("enter DigestScheme.createCnonce()");

    String cnonce;
    final String digAlg = "MD5";
    MessageDigest md5Helper;

    try {
        md5Helper = MessageDigest.getInstance(digAlg);
    } catch (NoSuchAlgorithmException e) {
        throw new HttpClientError(
          "Unsupported algorithm in HTTP Digest authentication: "
           + digAlg);
    }

    cnonce = Long.toString(System.currentTimeMillis());
    cnonce = encode(md5Helper.digest(EncodingUtil.getAsciiBytes(cnonce)));

    return cnonce;
}
 
Example #6
Source File: SpNegoScheme.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Processes the Negotiate challenge.
 *
 * @param challenge the challenge string
 * @throws MalformedChallengeException is thrown if the authentication challenge is malformed
 */
public void processChallenge(final String challenge) throws MalformedChallengeException {
    String authScheme = AuthChallengeParser.extractScheme(challenge);
    if (!authScheme.equalsIgnoreCase(getSchemeName())) {
        throw new MalformedChallengeException("Invalid Negotiate challenge: " + challenge);
    }
    int spaceIndex = challenge.indexOf(' ');
    if (spaceIndex != -1) {
        // step 2: received server challenge
        serverToken = Base64.decodeBase64(EncodingUtil.getBytes(
                challenge.substring(spaceIndex).trim(), "ASCII"));
        this.state = TYPE2_MSG_RECEIVED;
    } else {
        this.serverToken = null;
        if (this.state == UNINITIATED) {
            this.state = INITIATED;
        } else {
            this.state = FAILED;
        }
    }
}
 
Example #7
Source File: PostMethod.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Generates a request entity from the post parameters, if present.  Calls
 * {@link EntityEnclosingMethod#generateRequestBody()} if parameters have not been set.
 * 
 * @since 3.0
 */
protected RequestEntity generateRequestEntity() {
    if (!this.params.isEmpty()) {
        // Use a ByteArrayRequestEntity instead of a StringRequestEntity.
        // This is to avoid potential encoding issues.  Form url encoded strings
        // are ASCII by definition but the content type may not be.  Treating the content
        // as bytes allows us to keep the current charset without worrying about how
        // this charset will effect the encoding of the form url encoded string.
        String content = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet());
        ByteArrayRequestEntity entity = new ByteArrayRequestEntity(
            EncodingUtil.getAsciiBytes(content),
            FORM_URL_ENCODED_CONTENT_TYPE
        );
        return entity;
    } else {
        return super.generateRequestEntity();
    }
}
 
Example #8
Source File: FilePart.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Write the disposition header to the output stream
 * @param out The output stream
 * @throws IOException If an IO problem occurs
 * @see Part#sendDispositionHeader(OutputStream)
 */
protected void sendDispositionHeader(OutputStream out) 
throws IOException {
    LOG.trace("enter sendDispositionHeader(OutputStream out)");
    super.sendDispositionHeader(out);
    String filename = this.source.getFileName();
    if (filename != null) {
        out.write(FILE_NAME_BYTES);
        out.write(QUOTE_BYTES);
        out.write(EncodingUtil.getAsciiBytes(filename));
        out.write(QUOTE_BYTES);
    }
}
 
Example #9
Source File: LaxURI.java    From webarchive-commons with Apache License 2.0 5 votes vote down vote up
protected static String decode(String component, String charset)
        throws URIException {
    if (component == null) {
        throw new IllegalArgumentException(
                "Component array of chars may not be null");
    }
    byte[] rawdata = null;
    //     try {
    rawdata = LaxURLCodec.decodeUrlLoose(EncodingUtil
            .getAsciiBytes(component));
    //     } catch (DecoderException e) {
    //         throw new URIException(e.getMessage());
    //     }
    return EncodingUtil.getString(rawdata, charset);
}
 
Example #10
Source File: NTLM.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** 
 * Extracts the server nonce out of the given message type 2.
 * 
 * @param message the String containing the base64 encoded message.
 * @return an array of 8 bytes that the server sent to be used when
 * hashing the password.
 */
public byte[] parseType2Message(String message) {
    // Decode the message first.
    byte[] msg = Base64.decodeBase64(EncodingUtil.getBytes(message, DEFAULT_CHARSET));
    byte[] nonce = new byte[8];
    // The nonce is the 8 bytes starting from the byte in position 24.
    for (int i = 0; i < 8; i++) {
        nonce[i] = msg[i + 24];
    }
    return nonce;
}
 
Example #11
Source File: NTLM.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** 
 * Returns the response that has been generated after shrinking the array if
 * required and base64 encodes the response.
 * @return The response as above.
 */
private String getResponse() {
    byte[] resp;
    if (currentResponse.length > currentPosition) {
        byte[] tmp = new byte[currentPosition];
        for (int i = 0; i < currentPosition; i++) {
            tmp[i] = currentResponse[i];
        }
        resp = tmp;
    } else {
        resp = currentResponse;
    }
    return EncodingUtil.getAsciiString(Base64.encodeBase64(resp));
}
 
Example #12
Source File: ChunkedOutputStream.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Writes the cache out onto the underlying stream
 * @throws IOException
 * 
 * @since 3.0
 */
protected void flushCache() throws IOException {
    if (cachePosition > 0) {
        byte chunkHeader[] = EncodingUtil.getAsciiBytes(
                Integer.toHexString(cachePosition) + "\r\n");
        stream.write(chunkHeader, 0, chunkHeader.length);
        stream.write(cache, 0, cachePosition);
        stream.write(ENDCHUNK, 0, ENDCHUNK.length);
        cachePosition = 0;
    }
}
 
Example #13
Source File: Part.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Write the content transfer encoding header to the specified 
 * output stream
 * 
 * @param out The output stream
 * @throws IOException If an IO problem occurs.
 */
 protected void sendTransferEncodingHeader(OutputStream out) throws IOException {
    LOG.trace("enter sendTransferEncodingHeader(OutputStream out)");
    String transferEncoding = getTransferEncoding();
    if (transferEncoding != null) {
        out.write(CRLF_BYTES);
        out.write(CONTENT_TRANSFER_ENCODING_BYTES);
        out.write(EncodingUtil.getAsciiBytes(transferEncoding));
    }
}
 
Example #14
Source File: Part.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Write the content type header to the specified output stream
 * @param out The output stream
 * @throws IOException If an IO problem occurs.
 */
 protected void sendContentTypeHeader(OutputStream out) throws IOException {
    LOG.trace("enter sendContentTypeHeader(OutputStream out)");
    String contentType = getContentType();
    if (contentType != null) {
        out.write(CRLF_BYTES);
        out.write(CONTENT_TYPE_BYTES);
        out.write(EncodingUtil.getAsciiBytes(contentType));
        String charSet = getCharSet();
        if (charSet != null) {
            out.write(CHARSET_BYTES);
            out.write(EncodingUtil.getAsciiBytes(charSet));
        }
    }
}
 
Example #15
Source File: Part.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Write the content disposition header to the specified output stream
 * 
 * @param out The output stream
 * @throws IOException If an IO problem occurs.
 */
protected void sendDispositionHeader(OutputStream out) throws IOException {
    LOG.trace("enter sendDispositionHeader(OutputStream out)");
    out.write(CONTENT_DISPOSITION_BYTES);
    out.write(QUOTE_BYTES);
    out.write(EncodingUtil.getAsciiBytes(getName()));
    out.write(QUOTE_BYTES);
}
 
Example #16
Source File: StringPart.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Gets the content in bytes.  Bytes are lazily created to allow the charset to be changed
 * after the part is created.
 * 
 * @return the content in bytes
 */
private byte[] getContent() {
    if (content == null) {
        content = EncodingUtil.getBytes(value, getCharSet());
    }
    return content;
}
 
Example #17
Source File: MultipartRequestEntity.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns the MIME boundary string that is used to demarcate boundaries of
 * this part. The first call to this method will implicitly create a new
 * boundary string. To create a boundary string first the 
 * HttpMethodParams.MULTIPART_BOUNDARY parameter is considered. Otherwise 
 * a random one is generated.
 * 
 * @return The boundary string of this entity in ASCII encoding.
 */
protected byte[] getMultipartBoundary() {
    if (multipartBoundary == null) {
        String temp = (String) params.getParameter(HttpMethodParams.MULTIPART_BOUNDARY);
        if (temp != null) {
            multipartBoundary = EncodingUtil.getAsciiBytes(temp);
        } else {
            multipartBoundary = generateMultipartBoundary();
        }
    }
    return multipartBoundary;
}
 
Example #18
Source File: HttpParser.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Read up to <tt>"\n"</tt> from an (unchunked) input stream.
 * If the stream ends before the line terminator is found,
 * the last part of the string will still be returned.
 * If no input data available, <code>null</code> is returned.
 *
 * @param inputStream the stream to read from
 * @param charset charset of HTTP protocol elements
 *
 * @throws IOException if an I/O problem occurs
 * @return a line from the stream
 * 
 * @since 3.0
 */
public static String readLine(InputStream inputStream, String charset) throws IOException {
    LOG.trace("enter HttpParser.readLine(InputStream, String)");
    byte[] rawdata = readRawLine(inputStream);
    if (rawdata == null) {
        return null;
    }
    // strip CR and LF from the end
    int len = rawdata.length;
    int offset = 0;
    if (len > 0) {
        if (rawdata[len - 1] == '\n') {
            offset++;
            if (len > 1) {
                if (rawdata[len - 2] == '\r') {
                    offset++;
                }
            }
        }
    }
    final String result =
        EncodingUtil.getString(rawdata, 0, len - offset, charset);
    if (Wire.HEADER_WIRE.enabled()) {
        String logoutput = result;
        if (offset == 2)
            logoutput = result + "\r\n";
        else if (offset == 1)
            logoutput = result + "\n";
        Wire.HEADER_WIRE.input(logoutput);
    }
    return result;
}
 
Example #19
Source File: ContentDataPart.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Write the disposition header to the output stream
 * @param out The output stream
 * @throws IOException If an IO problem occurs
 * @see org.apache.commons.httpclient.methods.multipart.Part#sendDispositionHeader(OutputStream)
 */
protected void sendDispositionHeader(OutputStream out) 
throws IOException {
    super.sendDispositionHeader(out);
    if (filename != null) {
        out.write(FILE_NAME_BYTES);
        out.write(QUOTE_BYTES);
        out.write(EncodingUtil.getAsciiBytes(filename));
        out.write(QUOTE_BYTES);
    }
}
 
Example #20
Source File: OldHttpClientApi.java    From javabase with Apache License 2.0 5 votes vote down vote up
public void  get2(){
    String requesturl = "http://www.tuling123.com/openapi/api";
    GetMethod getMethod = new GetMethod(requesturl);
    try {
        String APIKEY = "4b441cb500f431adc6cc0cb650b4a5d0";
        String INFO ="北京时间";

        //get  参数
        NameValuePair nvp1=new NameValuePair("key",APIKEY);
        NameValuePair nvp2=new NameValuePair("info",INFO);
        NameValuePair[] nvp = new NameValuePair[2];
        nvp[0]=nvp1;
        nvp[1]=nvp2;
        String params=EncodingUtil.formUrlEncode(nvp, "UTF-8");
        //设置参数
        getMethod.setQueryString(params);

        byte[] responseBody = executeMethod(getMethod,10000);

         String content=new String(responseBody ,"utf-8");
        log.info(content);
    }catch (Exception e){
       log.error(""+e.getLocalizedMessage());
    }finally {
        //结束一定要关闭
        getMethod.releaseConnection();
    }
}
 
Example #21
Source File: HttpClientUtil.java    From javabase with Apache License 2.0 5 votes vote down vote up
/**
 * 转换成queryString
 * @param params
 *            存档参数名与参数值的集合
 * @return queryString
 */
public static String getQueryString(Map<String, String> params) {
	String queryString = null;
	if (params != null) {
		NameValuePair[] nvp = new NameValuePair[params.size()];
		int index = 0;
		for(String key : params.keySet()) {
			nvp[index++] = new NameValuePair(key, params.get(key));
		}
		queryString = EncodingUtil.formUrlEncode(nvp, "UTF-8");
	}
	return queryString == null ? "" : queryString;
}
 
Example #22
Source File: HeaderedArchiveRecord.java    From webarchive-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Read header if present. Technique borrowed from HttpClient HttpParse
 * class. Using http parser code for now. Later move to more generic header
 * parsing code if there proves a need.
 * 
 * @return ByteArrayInputStream with the http header in it or null if no
 *         http header.
 * @throws IOException
 */
private InputStream readContentHeaders() throws IOException {
    // If judged a record that doesn't have an http header, return
    // immediately.
    if (!hasContentHeaders()) {
        return null;
    }
    byte [] statusBytes = LaxHttpParser.readRawLine(getIn());
    int eolCharCount = getEolCharsCount(statusBytes);
    if (eolCharCount <= 0) {
        throw new IOException("Failed to read raw lie where one " +
            " was expected: " + new String(statusBytes));
    }
    String statusLine = EncodingUtil.getString(statusBytes, 0,
        statusBytes.length - eolCharCount, ARCConstants.DEFAULT_ENCODING);
    if (statusLine == null) {
        throw new NullPointerException("Expected status line is null");
    }
    // TODO: Tighten up this test.
    boolean isHttpResponse = StatusLine.startsWithHTTP(statusLine);
    boolean isHttpRequest = false;
    if (!isHttpResponse) {
        isHttpRequest = statusLine.toUpperCase().startsWith("GET") ||
            !statusLine.toUpperCase().startsWith("POST");
    }
    if (!isHttpResponse && !isHttpRequest) {
        throw new UnexpectedStartLineIOException("Failed parse of " +
            "status line: " + statusLine);
    }
    this.statusCode = isHttpResponse?
        (new StatusLine(statusLine)).getStatusCode(): -1;
    
    // Save off all bytes read.  Keep them as bytes rather than
    // convert to strings so we don't have to worry about encodings
    // though this should never be a problem doing http headers since
    // its all supposed to be ascii.
    ByteArrayOutputStream baos =
        new ByteArrayOutputStream(statusBytes.length + 4 * 1024);
    baos.write(statusBytes);
    
    // Now read rest of the header lines looking for the separation
    // between header and body.
    for (byte [] lineBytes = null; true;) {
        lineBytes = LaxHttpParser.readRawLine(getIn());
        eolCharCount = getEolCharsCount(lineBytes);
        if (eolCharCount <= 0) {
            throw new IOException("Failed reading headers: " +
                ((lineBytes != null)? new String(lineBytes): null));
        }
        // Save the bytes read.
        baos.write(lineBytes);
        if ((lineBytes.length - eolCharCount) <= 0) {
            // We've finished reading the http header.
            break;
        }
    }
    
    byte [] headerBytes = baos.toByteArray();
    // Save off where content body, post content headers, starts.
    this.contentHeadersLength = headerBytes.length;
    ByteArrayInputStream bais =
        new ByteArrayInputStream(headerBytes);
    if (!bais.markSupported()) {
        throw new IOException("ByteArrayInputStream does not support mark");
    }
    bais.mark(headerBytes.length);
    // Read the status line.  Don't let it into the parseHeaders function.
    // It doesn't know what to do with it.
    bais.read(statusBytes, 0, statusBytes.length);
    this.contentHeaders = LaxHttpParser.parseHeaders(bais,
        ARCConstants.DEFAULT_ENCODING);
    bais.reset();
    return bais;
}
 
Example #23
Source File: MultipartRequestEntity.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public String getContentType() {
    StringBuffer buffer = new StringBuffer(MULTIPART_FORM_CONTENT_TYPE);
    buffer.append("; boundary=");
    buffer.append(EncodingUtil.getAsciiString(getMultipartBoundary()));
    return buffer.toString();
}
 
Example #24
Source File: NTLM.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/** 
 * Creates the LANManager and NT response for the given password using the
 * given nonce.
 * @param password the password to create a hash for.
 * @param nonce the nonce sent by the server.
 * @return The response.
 * @throws HttpException If {@link #encrypt(byte[],byte[])} fails.
 */
private byte[] hashPassword(String password, byte[] nonce)
    throws AuthenticationException {
    byte[] passw = EncodingUtil.getBytes(password.toUpperCase(), credentialCharset);
    byte[] lmPw1 = new byte[7];
    byte[] lmPw2 = new byte[7];

    int len = passw.length;
    if (len > 7) {
        len = 7;
    }

    int idx;
    for (idx = 0; idx < len; idx++) {
        lmPw1[idx] = passw[idx];
    }
    for (; idx < 7; idx++) {
        lmPw1[idx] = (byte) 0;
    }

    len = passw.length;
    if (len > 14) {
        len = 14;
    }
    for (idx = 7; idx < len; idx++) {
        lmPw2[idx - 7] = passw[idx];
    }
    for (; idx < 14; idx++) {
        lmPw2[idx - 7] = (byte) 0;
    }

    // Create LanManager hashed Password
    byte[] magic = {
        (byte) 0x4B, (byte) 0x47, (byte) 0x53, (byte) 0x21, 
        (byte) 0x40, (byte) 0x23, (byte) 0x24, (byte) 0x25
    };

    byte[] lmHpw1;
    lmHpw1 = encrypt(lmPw1, magic);

    byte[] lmHpw2 = encrypt(lmPw2, magic);

    byte[] lmHpw = new byte[21];
    for (int i = 0; i < lmHpw1.length; i++) {
        lmHpw[i] = lmHpw1[i];
    }
    for (int i = 0; i < lmHpw2.length; i++) {
        lmHpw[i + 8] = lmHpw2[i];
    }
    for (int i = 0; i < 5; i++) {
        lmHpw[i + 16] = (byte) 0;
    }

    // Create the responses.
    byte[] lmResp = new byte[24];
    calcResp(lmHpw, nonce, lmResp);

    return lmResp;
}
 
Example #25
Source File: NTLM.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Creates the first message (type 1 message) in the NTLM authentication sequence.
 * This message includes the user name, domain and host for the authentication session.
 *
 * @param host the computer name of the host requesting authentication.
 * @param domain The domain to authenticate with.
 * @return String the message to add to the HTTP request header.
 */
public String getType1Message(String host, String domain) {
    host = host.toUpperCase();
    domain = domain.toUpperCase();
    byte[] hostBytes = EncodingUtil.getBytes(host, DEFAULT_CHARSET);
    byte[] domainBytes = EncodingUtil.getBytes(domain, DEFAULT_CHARSET);

    int finalLength = 32 + hostBytes.length + domainBytes.length;
    prepareResponse(finalLength);
    
    // The initial id string.
    byte[] protocol = EncodingUtil.getBytes("NTLMSSP", DEFAULT_CHARSET);
    addBytes(protocol);
    addByte((byte) 0);

    // Type
    addByte((byte) 1);
    addByte((byte) 0);
    addByte((byte) 0);
    addByte((byte) 0);

    // Flags
    addByte((byte) 6);
    addByte((byte) 82);
    addByte((byte) 0);
    addByte((byte) 0);

    // Domain length (first time).
    int iDomLen = domainBytes.length;
    byte[] domLen = convertShort(iDomLen);
    addByte(domLen[0]);
    addByte(domLen[1]);

    // Domain length (second time).
    addByte(domLen[0]);
    addByte(domLen[1]);

    // Domain offset.
    byte[] domOff = convertShort(hostBytes.length + 32);
    addByte(domOff[0]);
    addByte(domOff[1]);
    addByte((byte) 0);
    addByte((byte) 0);

    // Host length (first time).
    byte[] hostLen = convertShort(hostBytes.length);
    addByte(hostLen[0]);
    addByte(hostLen[1]);

    // Host length (second time).
    addByte(hostLen[0]);
    addByte(hostLen[1]);

    // Host offset (always 32).
    byte[] hostOff = convertShort(32);
    addByte(hostOff[0]);
    addByte(hostOff[1]);
    addByte((byte) 0);
    addByte((byte) 0);

    // Host String.
    addBytes(hostBytes);

    // Domain String.
    addBytes(domainBytes);

    return getResponse();
}
 
Example #26
Source File: ChunkedInputStream.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Expects the stream to start with a chunksize in hex with optional
 * comments after a semicolon. The line must end with a CRLF: "a3; some
 * comment\r\n" Positions the stream at the start of the next line.
 *
 * @param in The new input stream.
 * @param required <tt>true<tt/> if a valid chunk must be present,
 *                 <tt>false<tt/> otherwise.
 * 
 * @return the chunk size as integer
 * 
 * @throws IOException when the chunk size could not be parsed
 */
private static int getChunkSizeFromInputStream(final InputStream in) 
  throws IOException {
        
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // States: 0=normal, 1=\r was scanned, 2=inside quoted string, -1=end
    int state = 0; 
    while (state != -1) {
    int b = in.read();
        if (b == -1) { 
            throw new IOException("chunked stream ended unexpectedly");
        }
        switch (state) {
            case 0: 
                switch (b) {
                    case '\r':
                        state = 1;
                        break;
                    case '\"':
                        state = 2;
                        /* fall through */
                    default:
                        baos.write(b);
                }
                break;

            case 1:
                if (b == '\n') {
                    state = -1;
                } else {
                    // this was not CRLF
                    throw new IOException("Protocol violation: Unexpected"
                        + " single newline character in chunk size");
                }
                break;

            case 2:
                switch (b) {
                    case '\\':
                        b = in.read();
                        baos.write(b);
                        break;
                    case '\"':
                        state = 0;
                        /* fall through */
                    default:
                        baos.write(b);
                }
                break;
            default: throw new RuntimeException("assertion failed");
        }
    }

    //parse data
    String dataString = EncodingUtil.getAsciiString(baos.toByteArray());
    int separator = dataString.indexOf(';');
    dataString = (separator > 0)
        ? dataString.substring(0, separator).trim()
        : dataString.trim();

    int result;
    try {
        result = Integer.parseInt(dataString.trim(), 16);
    } catch (NumberFormatException e) {
        throw new IOException ("Bad chunk size: " + dataString);
    }
    return result;
}
 
Example #27
Source File: HttpMethodBase.java    From http4e with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the response body of the HTTP method, if any, as a {@link String}. 
 * If response body is not available or cannot be read, returns <tt>null</tt>
 * The string conversion on the data is done using the character encoding specified
 * in <tt>Content-Type</tt> header.
 * 
 * Note: This will cause the entire response body to be buffered in memory. A
 * malicious server may easily exhaust all the VM memory. It is strongly
 * recommended, to use getResponseAsStream if the content length of the response
 * is unknown or resonably large.
 * 
 * @return The response body.
 * 
 * @throws IOException If an I/O (transport) problem occurs while obtaining the 
 * response body.
 */
public String getResponseBodyAsString() throws IOException {
    byte[] rawdata = null;
    if (responseAvailable()) {
        rawdata = getResponseBody();
    }
    if (rawdata != null) {
        return EncodingUtil.getString(rawdata, getResponseCharSet());
    } else {
        return null;
    }
}
 
Example #28
Source File: URI.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Decodes URI encoded string.
 *
 * This is a two mapping, one from URI characters to octets, and
 * subsequently a second from octets to original characters:
 * <p><blockquote><pre>
 *   URI character sequence->octet sequence->original character sequence
 * </pre></blockquote><p>
 *
 * A URI must be separated into its components before the escaped
 * characters within those components can be allowedly decoded.
 * <p>
 * Notice that there is a chance that URI characters that are non UTF-8
 * may be parsed as valid UTF-8.  A recent non-scientific analysis found
 * that EUC encoded Japanese words had a 2.7% false reading; SJIS had a
 * 0.0005% false reading; other encoding such as ASCII or KOI-8 have a 0%
 * false reading.
 * <p>
 * The percent "%" character always has the reserved purpose of being
 * the escape indicator, it must be escaped as "%25" in order to be used
 * as data within a URI.
 * <p>
 * The unescape method is internally performed within this method.
 *
 * @param component the URI character sequence
 * @param charset the protocol charset
 * @return original character sequence
 * @throws URIException incomplete trailing escape pattern or unsupported
 * character encoding
 * 
 * @since 3.0
 */
protected static String decode(String component, String charset) 
    throws URIException {
    if (component == null) {
        throw new IllegalArgumentException("Component array of chars may not be null");
    }
    byte[] rawdata = null;
    try { 
        rawdata = URLCodec.decodeUrl(EncodingUtil.getAsciiBytes(component));
    } catch (DecoderException e) {
        throw new URIException(e.getMessage());
    }
    return EncodingUtil.getString(rawdata, charset);
}
 
Example #29
Source File: URI.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Encodes URI string.
 *
 * This is a two mapping, one from original characters to octets, and
 * subsequently a second from octets to URI characters:
 * <p><blockquote><pre>
 *   original character sequence->octet sequence->URI character sequence
 * </pre></blockquote><p>
 *
 * An escaped octet is encoded as a character triplet, consisting of the
 * percent character "%" followed by the two hexadecimal digits
 * representing the octet code. For example, "%20" is the escaped
 * encoding for the US-ASCII space character.
 * <p>
 * Conversion from the local filesystem character set to UTF-8 will
 * normally involve a two step process. First convert the local character
 * set to the UCS; then convert the UCS to UTF-8.
 * The first step in the process can be performed by maintaining a mapping
 * table that includes the local character set code and the corresponding
 * UCS code.
 * The next step is to convert the UCS character code to the UTF-8 encoding.
 * <p>
 * Mapping between vendor codepages can be done in a very similar manner
 * as described above.
 * <p>
 * The only time escape encodings can allowedly be made is when a URI is
 * being created from its component parts.  The escape and validate methods
 * are internally performed within this method.
 *
 * @param original the original character sequence
 * @param allowed those characters that are allowed within a component
 * @param charset the protocol charset
 * @return URI character sequence
 * @throws URIException null component or unsupported character encoding
 */
    
protected static char[] encode(String original, BitSet allowed,
        String charset) throws URIException {
    if (original == null) {
        throw new IllegalArgumentException("Original string may not be null");
    }
    if (allowed == null) {
        throw new IllegalArgumentException("Allowed bitset may not be null");
    }
    byte[] rawdata = URLCodec.encodeUrl(allowed, EncodingUtil.getBytes(original, charset));
    return EncodingUtil.getAsciiString(rawdata).toCharArray();
}
 
Example #30
Source File: HttpMethodBase.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Returns the response body of the HTTP method, if any, as a {@link String}. 
 * If response body is not available or cannot be read, returns <tt>null</tt>
 * The string conversion on the data is done using the character encoding specified
 * in <tt>Content-Type</tt> header. Buffers the response and this method can be 
 * called several times yielding the same result each time.</p>
 * 
 * Note: This will cause the entire response body to be buffered in memory. This method is
 * safe if the content length of the response is unknown, because the amount of memory used
 * is limited.<p>
 * 
 * If the response is large this method involves lots of array copying and many object 
 * allocations, which makes it unsuitable for high-performance / low-footprint applications.
 * Those applications should use {@link #getResponseBodyAsStream()}.
 * 
 * @param maxlen the maximum content length to accept (number of bytes). Note that,
 * depending on the encoding, this is not equal to the number of characters.
 * @return The response body or <code>null</code>.
 * 
 * @throws IOException If an I/O (transport) problem occurs while obtaining the 
 * response body.
 */
public String getResponseBodyAsString(int maxlen) throws IOException {
    if (maxlen < 0) throw new IllegalArgumentException("maxlen must be positive");
    byte[] rawdata = null;
    if (responseAvailable()) {
        rawdata = getResponseBody(maxlen);
    }
    if (rawdata != null) {
        return EncodingUtil.getString(rawdata, getResponseCharSet());
    } else {
        return null;
    }
}