/*
 * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
 *
 * This file is part of Resin(R) Open Source
 *
 * Each copy or derived work must preserve the copyright notice and this
 * notice unmodified.
 *
 * Resin Open Source is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Resin Open Source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
 * of NON-INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Resin Open Source; if not, write to the
 *
 *   Free Software Foundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307  USA
 *
 * @author Scott Ferguson
 */

package com.caucho.quercus.lib;

import java.io.Reader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.LineNumberReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import java.net.URL;
import java.net.Socket;

import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.LinkedHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.caucho.util.L10N;
import com.caucho.util.Base64;
import com.caucho.util.CharBuffer;
import com.caucho.util.URLUtil;

import com.caucho.vfs.Path;
import com.caucho.vfs.TempBuffer;

import com.caucho.quercus.module.AbstractQuercusModule;
import com.caucho.quercus.module.Optional;

import com.caucho.quercus.lib.file.FileModule;
import com.caucho.quercus.lib.file.BinaryInput;
import com.caucho.quercus.lib.file.BinaryStream;

import com.caucho.quercus.env.Env;
import com.caucho.quercus.env.Value;
import com.caucho.quercus.env.NullValue;
import com.caucho.quercus.env.LongValue;
import com.caucho.quercus.env.UnsetValue;
import com.caucho.quercus.env.StringValueImpl;
import com.caucho.quercus.env.StringBuilderValue;
import com.caucho.quercus.env.ArrayValue;
import com.caucho.quercus.env.ArrayValueImpl;
import com.caucho.quercus.env.ObjectValue;
import com.caucho.quercus.env.BooleanValue;

/**
 * PHP URL
 */
public class UrlModule extends AbstractQuercusModule {
  private static final L10N L = new L10N(UrlModule.class);
  private static final Logger log 
    = Logger.getLogger(UrlModule.class.getName());

  /**
   * Encodes base64
   */
  public static String base64_encode(InputStream is)
  {
    CharBuffer cb = new CharBuffer();

    TempBuffer tb = TempBuffer.allocate();
    byte []buffer = tb.getBuffer();

    int len;
    int offset = 0;

    try {
      while ((len = is.read(buffer, offset, buffer.length)) >= 0) {
	int tail = len % 3;
	
	Base64.encode(cb, buffer, 0, len - tail);

	System.arraycopy(buffer, len - tail, buffer, 0, tail);
	offset = tail;
      }

      if (offset > 0)
	Base64.encode(cb, buffer, 0, offset);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    TempBuffer.free(tb);

    return cb.toString();
  }

  /**
   * Decodes base64
   */
  public static String base64_decode(String str)
  {
    return Base64.decode(str);
  }

  /**
   * Creates a http string.
   */
  public String http_build_query(Value value,
				 @Optional String prefix)
  {
    StringBuilder sb = new StringBuilder();

    int index = 0;
    if (value instanceof ArrayValue) {
      ArrayValue array = (ArrayValue) value;

      for (Map.Entry<Value,Value> entry : array.entrySet()) {
	Value keyValue = entry.getKey();
	Value v = entry.getValue();

	String key;

	if (keyValue.isLongConvertible())
	  key = prefix + keyValue;
	else
	  key = keyValue.toString();

	if (v instanceof ArrayValue)
	  http_build_query(sb, key, (ArrayValue) v);
	else {
	  if (sb.length() > 0)
	    sb.append('&');

	  sb.append(key);
	  sb.append('=');
	  urlencode(sb, v.toString());
	}
      }
    }

    return sb.toString();
  }

  /**
   * Creates a http string.
   */
  private void http_build_query(StringBuilder sb,
				String prefix,
				ArrayValue array)
  {
    for (Map.Entry<Value,Value> entry : array.entrySet()) {
      Value keyValue = entry.getKey();
      Value v = entry.getValue();

      String key = prefix + '[' + keyValue + ']';

      if (v instanceof ArrayValue)
	http_build_query(sb, key, (ArrayValue) v);
      else {
	if (sb.length() > 0)
	  sb.append('&');

	sb.append(key);
	sb.append('=');
	urlencode(sb, v.toString());
      }
    }
  }

  /**
   * Gets the magic quotes value.
   */
  public static String urlencode(String str)
  {
    StringBuilder sb = new StringBuilder();

    urlencode(sb, str);

    return sb.toString();
  }

  /**
   * Gets the magic quotes value.
   */
  private static void urlencode(StringBuilder sb, String str)
  {
    int len = str.length();

    for (int i = 0; i < len; i++) {
      char ch = str.charAt(i);

      if ('a' <= ch && ch <= 'z')
	sb.append(ch);
      else if ('A' <= ch && ch <= 'Z')
	sb.append(ch);
      else if ('0' <= ch && ch <= '9')
	sb.append(ch);
      else if (ch == '-' || ch == '_' || ch == '.')
	sb.append(ch);
      else if (ch == ' ')
	sb.append('+');
      else {
	sb.append('%');
	sb.append(toHexDigit(ch / 16));
	sb.append(toHexDigit(ch));
      }
    }
  }

  /**
   * Returns the decoded string.
   */
  public static String urldecode(String s)
  {
    int len = s.length();
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < len; i++) {
      char ch = s.charAt(i);

      if (ch == '%' && i + 2 < len) {
        int d1 = s.charAt(i + 1);
        int d2 = s.charAt(i + 2);

        int v = 0;

        if ('0' <= d1 && d1 <= '9')
          v = 16 * (d1 - '0');
        else if ('a' <= d1 && d1 <= 'f')
          v = 16 * (d1 - 'a' + 10);
        else if ('A' <= d1 && d1 <= 'F')
          v = 16 * (d1 - 'A' + 10);
        else {
          sb.append('%');
          continue;
        }

        if ('0' <= d2 && d2 <= '9')
          v += (d2 - '0');
        else if ('a' <= d2 && d2 <= 'f')
          v += (d2 - 'a' + 10);
        else if ('A' <= d2 && d2 <= 'F')
          v += (d2 - 'A' + 10);
        else {
          sb.append('%');
          continue;
        }

        i += 2;
        sb.append((char) v);
      }
      else if (ch == '+')
	sb.append(' ');
      else
        sb.append(ch);
    }

    return sb.toString();
  }

  /**
   * Returns the decoded string.
   */
  public static String rawurldecode(String s)
  {
    int len = s.length();
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < len; i++) {
      char ch = s.charAt(i);

      if (ch == '%' && i + 2 < len) {
        int d1 = s.charAt(i + 1);
        int d2 = s.charAt(i + 2);

        int v = 0;

        if ('0' <= d1 && d1 <= '9')
          v = 16 * (d1 - '0');
        else if ('a' <= d1 && d1 <= 'f')
          v = 16 * (d1 - 'a' + 10);
        else if ('A' <= d1 && d1 <= 'F')
          v = 16 * (d1 - 'A' + 10);
        else {
          sb.append('%');
          continue;
        }

        if ('0' <= d2 && d2 <= '9')
          v += (d2 - '0');
        else if ('a' <= d2 && d2 <= 'f')
          v += (d2 - 'a' + 10);
        else if ('A' <= d2 && d2 <= 'F')
          v += (d2 - 'A' + 10);
        else {
          sb.append('%');
          continue;
        }

        i += 2;
        sb.append((char) v);
      }
      else
        sb.append(ch);
    }

    return sb.toString();
  }

  /**
   * Encodes the url
   */
  public static String rawurlencode(String str)
  {
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < str.length(); i++) {
      char ch = str.charAt(i);

      if ('a' <= ch && ch <= 'z' ||
          'A' <= ch && ch <= 'Z' ||
          '0' <= ch && ch <= '9' ||
          ch == '-' || ch == '_' || ch == '.') {
        sb.append(ch);
      }
      else {
        sb.append('%');
        sb.append(toHexDigit(ch >> 4));
        sb.append(toHexDigit(ch));
      }
    }

    return sb.toString();
  }

  enum ParseUrlState {
    INIT, USER, PASS, HOST, PORT, PATH, QUERY, FRAGMENT
  };

  /**
   * Parses the URL into an array.
   */
  public static Value parse_url(Env env, String str)
  {
    int i = 0;
    int length = str.length();

    CharBuffer sb = new CharBuffer();

    ArrayValueImpl value = new ArrayValueImpl();

    ParseUrlState state = ParseUrlState.INIT;

    String user = null;

    for (; i < length; i++) {
      char ch = str.charAt(i);

      switch (ch) {
      case ':':
	if (state == ParseUrlState.INIT) {
	  value.put("scheme", sb.toString());
	  sb.clear();

	  if (length <= i + 1 || str.charAt(i + 1) != '/') {
	    state = ParseUrlState.PATH;
	  }
	  else if (length <= i + 2 || str.charAt(i + 2) != '/') {
	    state = ParseUrlState.PATH;
	  }
	  else if (length <= i + 3 || str.charAt(i + 3) != '/') {
	    i += 2;
	    state = ParseUrlState.USER;
	  }
	  else {
	    // file:///foo

	    i += 2;
	    state = ParseUrlState.PATH;
	  }
	}
	else if (state == ParseUrlState.USER) {
	  user = sb.toString();
	  sb.clear();
	  state = ParseUrlState.PASS;
	}
	else if (state == ParseUrlState.HOST) {
	  value.put("host", sb.toString());
	  sb.clear();
	  state = ParseUrlState.PORT;
	}
	else
	  sb.append(ch);
	break;

      case '@':
	if (state == ParseUrlState.USER) {
	  value.put("user", sb.toString());
	  sb.clear();
	  state = ParseUrlState.HOST;
	}
	else if (state == ParseUrlState.PASS) {
	  value.put("user", user);
	  value.put("pass", sb.toString());
	  sb.clear();
	  state = ParseUrlState.HOST;
	}
	else
	  sb.append(ch);
	break;

      case '/':
	if (state == ParseUrlState.USER || state == ParseUrlState.HOST) {
	  value.put("host", sb.toString());
	  sb.clear();
	  state = ParseUrlState.PATH;
	  sb.append(ch);
	}
	else if (state == ParseUrlState.PASS) {
	  value.put("host", user);
	  value.put(new StringValueImpl("port"),
		    new LongValue(new StringValueImpl(sb.toString()).toLong()));
	  sb.clear();
	  state = ParseUrlState.PATH;
	  sb.append(ch);
	}
	else if (state == ParseUrlState.PORT) {
	  value.put(new StringValueImpl("port"),
		    new LongValue(new StringValueImpl(sb.toString()).toLong()));
	  sb.clear();
	  state = ParseUrlState.PATH;
	  sb.append(ch);
	}
	else
	  sb.append(ch);
	break;

      case '?':
	if (state == ParseUrlState.USER || state == ParseUrlState.HOST) {
	  value.put("host", sb.toString());
	  sb.clear();
	  state = ParseUrlState.QUERY;
	}
	else if (state == ParseUrlState.PASS) {
	  value.put("host", user);
	  value.put(new StringValueImpl("port"),
		    new LongValue(new StringValueImpl(sb.toString()).toLong()));
	  sb.clear();
	  state = ParseUrlState.QUERY;
	}
	else if (state == ParseUrlState.PORT) {
	  value.put(new StringValueImpl("port"),
		    new LongValue(new StringValueImpl(sb.toString()).toLong()));
	  sb.clear();
	  state = ParseUrlState.QUERY;
	}
	else if (state == ParseUrlState.PATH) {
	  if (sb.length() > 0)
	    value.put("path", sb.toString());
	  sb.clear();
	  state = ParseUrlState.QUERY;
	}
	else
	  sb.append(ch);
	break;

      case '#':
	if (state == ParseUrlState.USER || state == ParseUrlState.HOST) {
	  value.put("host", sb.toString());
	  sb.clear();
	  state = ParseUrlState.FRAGMENT;
	}
	else if (state == ParseUrlState.PASS) {
	  value.put("host", user);
	  value.put(new StringValueImpl("port"),
		    new LongValue(new StringValueImpl(sb.toString()).toLong()));
	  sb.clear();
	  state = ParseUrlState.FRAGMENT;
	}
	else if (state == ParseUrlState.PORT) {
	  value.put(new StringValueImpl("port"),
		    new LongValue(new StringValueImpl(sb.toString()).toLong()));
	  sb.clear();
	  state = ParseUrlState.FRAGMENT;
	}
	else if (state == ParseUrlState.PATH) {
	  if (sb.length() > 0)
	    value.put("path", sb.toString());
	  sb.clear();
	  state = ParseUrlState.FRAGMENT;
	}
	else if (state == ParseUrlState.QUERY) {
	  if (sb.length() > 0)
	    value.put("query", sb.toString());
	  sb.clear();
	  state = ParseUrlState.FRAGMENT;
	}
	else
	  sb.append(ch);
	break;

      default:
	sb.append((char) ch);
	break;
      }
    }

    if (sb.length() == 0) {
    }
    else if (state == ParseUrlState.USER ||
	     state == ParseUrlState.HOST)
      value.put("host", sb.toString());
    else if (state == ParseUrlState.PASS) {
      value.put("host", user);
      value.put(new StringValueImpl("port"),
		new LongValue(new StringValueImpl(sb.toString()).toLong()));
    }
    else if (state == ParseUrlState.PORT) {
      value.put(new StringValueImpl("port"),
		new LongValue(new StringValueImpl(sb.toString()).toLong()));
    }
    else if (state == ParseUrlState.QUERY)
      value.put("query", sb.toString());
    else if (state == ParseUrlState.FRAGMENT)
      value.put("fragment", sb.toString());
    else
      value.put("path", sb.toString());

    return value;
  }

  public static Value http_build_query(Env env, Value formdata, 
                                       @Optional String numeric_prefix)
  {
    String result = 
      httpBuildQueryImpl(env, "", formdata, numeric_prefix).toString();

    return new StringValueImpl(result);
  }

  public static StringBuilder httpBuildQueryImpl(Env env, String path, 
                                                 Value formdata, 
                                                 String numeric_prefix)
  {
    StringBuilder result = new StringBuilder();
    
    Set<Map.Entry<Value,Value>> entrySet;

    if (formdata.isArray())
      entrySet = ((ArrayValue)formdata).entrySet();
    else if (formdata.isObject()) {
      Set<Map.Entry<String,Value>> stringEntrySet
        = ((ObjectValue)formdata).entrySet();

      LinkedHashMap<Value,Value> valueMap = new LinkedHashMap<Value,Value>();

      for (Map.Entry<String,Value> entry : stringEntrySet)
        valueMap.put(new StringValueImpl(entry.getKey()), entry.getValue());

      entrySet = valueMap.entrySet();
    } else {
      env.warning(L.l("formdata must be an array or object"));

      return result; 
    }

    for (Map.Entry<Value,Value> entry : entrySet) {
      String newPath = makeNewPath(path, entry.getKey(), numeric_prefix);

      if (entry.getValue().isArray() || entry.getValue().isObject()) {
        // can always throw away the numeric prefix on recursive calls
        result.append(httpBuildQueryImpl(env, newPath, entry.getValue(), null));
        result.append("&");
      } else {
        result.append(newPath + "=");
        result.append(urlencode(entry.getValue().toString()));
        result.append("&");
      }
    }

    // trim any trailing &'s
    if (result.length() > 0) 
      result.deleteCharAt(result.length() - 1);

    return result;
  }

  private static String makeNewPath(String oldPath, Value key, 
                                    String numeric_prefix)
  {
    if (oldPath.length() == 0) {
      if (key.isLongConvertible() && numeric_prefix != null)
        return urlencode(numeric_prefix + key.toString());
      else
        return urlencode(key.toString());
    } else
      return oldPath + "[" + urlencode(key.toString()) + "]";
  }

  /**
   * Connects to the given URL using a HEAD request to retreive
   * the headers sent in the response.
   */
  public static Value get_headers(Env env, String urlString, 
                                  @Optional Value format)
  {
    Socket socket = null;
    
    try {
      URL url = new URL(urlString);

      if (! url.getProtocol().equals("http") && 
          ! url.getProtocol().equals("https")) {
        env.warning(L.l("Not an HTTP URL"));
        return null;
      }

      int port = 80;

      if (url.getPort() < 0) {
        if (url.getProtocol().equals("http"))
          port = 80;
        else if (url.getProtocol().equals("https"))
          port = 443;
      } else {
        port = url.getPort();
      }

      socket = new Socket(url.getHost(), port);
      
      OutputStream out = socket.getOutputStream();
      InputStream in = socket.getInputStream();

      StringBuilder request = new StringBuilder();

      request.append("HEAD ");

      if (url.getPath() != null)
        request.append(url.getPath());

      if (url.getQuery() != null) 
        request.append("?" + url.getQuery());

      if (url.getRef() != null)
        request.append("#" + url.getRef());

      request.append(" HTTP/1.0\r\n");

      if (url.getHost() != null)
        request.append("Host: " + url.getHost() + "\r\n");

      request.append("\r\n");

      OutputStreamWriter writer = new OutputStreamWriter(out);
      writer.write(request.toString());
      writer.flush();

      LineNumberReader reader = new LineNumberReader(new InputStreamReader(in));

      ArrayValue result = new ArrayValueImpl();

      if (format.toBoolean()) {
        for (String line = reader.readLine(); 
             line != null; 
             line = reader.readLine()) {
          line = line.trim();
          
          if (line.length() == 0)
            continue;

          int colon = line.indexOf(':');

          ArrayValue values;

          if (colon < 0)
            result.put(new StringValueImpl(line.trim()));
          else {
            StringValueImpl key = 
              new StringValueImpl(line.substring(0, colon).trim());

            StringValueImpl value;

            if (colon < line.length())
              value = new StringValueImpl(line.substring(colon + 1).trim());
            else
              value = new StringValueImpl("");


            if (result.get(key) != UnsetValue.UNSET)
              values = (ArrayValue)result.get(key);
            else {
              values = new ArrayValueImpl();

              result.put(key, values);
            }

            values.put(value);
          }
        }

        // collapse single entries
        for (Value key : result.keySet()) {
          Value value = result.get(key);

          if (value.isArray() && ((ArrayValue)value).getSize() == 1)
            result.put(key, ((ArrayValue)value).get(LongValue.ZERO));
        }
      } else {
        for (String line = reader.readLine(); 
            line != null; 
            line = reader.readLine()) {
          line = line.trim();

          if (line.length() == 0)
            continue; 

          result.put(new StringValueImpl(line.trim()));
        }
      }

      return result;
    } catch (Exception e) {
      env.warning(e);

      return BooleanValue.FALSE;
    } finally {
      try {
        if (socket != null)
          socket.close();
      } catch (IOException e) {
        env.warning(e);
      }
    }
  }

  /**
   * Extracts the meta tags from a file and returns them as an array.
   */
  public static Value get_meta_tags(Env env, String filename, 
                                    @Optional("false") boolean use_include_path)
  {
    InputStream in = null;

    ArrayValue result = new ArrayValueImpl();

    try {
      BinaryStream stream = 
        FileModule.fopen(env, filename, "r", use_include_path, null);

      if (stream == null || ! (stream instanceof BinaryInput))
        return result;

      BinaryInput input = (BinaryInput) stream;

      while (! input.isEOF()) {
        String tag = getNextTag(input);

        if (tag.equalsIgnoreCase("meta")) {
          String name = null;
          String content = null;

          String [] attr;

          while ((attr = getNextAttribute(input)) != null) {
            if (name == null && attr[0].equalsIgnoreCase("name")) {
              if (attr.length > 1)
                name = attr[1];
            } else if (content == null && attr[0].equalsIgnoreCase("content")) {
              if (attr.length > 1)
                content = attr[1];
            }

            if (name != null && content != null) {
              result.put(new StringValueImpl(name), 
                         new StringValueImpl(content));
              break;
            }
          }
        } else if (tag.equalsIgnoreCase("/head"))
          break;
      }
    } catch (IOException e) {
      env.warning(e);
    } finally {
      try {
        if (in != null)
          in.close();
      } catch (IOException e) {
        env.warning(e);
      }
    }

    return result;
  }

  private static String getNextTag(BinaryInput input)
    throws IOException
  {
    StringBuilder tag = new StringBuilder();

    for (int ch = 0; ! input.isEOF() && ch != '<'; ch = input.read()) {}

    while (! input.isEOF()) {
      int ch = input.read();

      if (Character.isWhitespace(ch))
        break;

      tag.append((char) ch);
    }

    return tag.toString();
  }

  /**
   * Finds the next attribute in the stream and return the key and value
   * as an array.
   */
  private static String [] getNextAttribute(BinaryInput input)
    throws IOException
  {
    int ch;

    consumeWhiteSpace(input);

    StringBuilder attribute = new StringBuilder();

    while (! input.isEOF()) {
      ch = input.read();

      if (isValidAttributeCharacter(ch))
        attribute.append((char) ch);
      else {
        input.unread();
        break;
      }
    }

    if (attribute.length() == 0)
      return null;

    consumeWhiteSpace(input);

    if (input.isEOF())
      return new String[] { attribute.toString() };
    
    ch = input.read();
    if (ch != '=') {
      input.unread();

      return new String[] { attribute.toString() };
    }

    consumeWhiteSpace(input);

    // check for quoting
    int quote = ' ';
    boolean quoted = false;

    if (input.isEOF())
      return new String[] { attribute.toString() };

    ch = input.read();

    if (ch == '"' || ch == '\'') {
      quoted = true;
      quote = ch;
    } else 
      input.unread();

    StringBuilder value = new StringBuilder();

    while (! input.isEOF()) {
      ch = input.read();
  
      // mimics PHP behavior
      if ((quoted && ch == quote) ||
          (! quoted && Character.isWhitespace(ch)) || ch == '>')
        break;

      value.append((char) ch);
    }

    return new String[] { attribute.toString(), value.toString() };
  }

  private static void consumeWhiteSpace(BinaryInput input)
    throws IOException
  {
    int ch = 0;

    while (! input.isEOF() && Character.isWhitespace(ch = input.read())) {}

    if (! Character.isWhitespace(ch))
      input.unread();
  }

  private static boolean isValidAttributeCharacter(int ch)
  {
    return Character.isLetterOrDigit(ch) || 
      (ch == '-') || (ch == '.') || (ch == '_') || (ch == ':');
  }

  private static char toHexDigit(int d)
  {
    d = d & 0xf;

    if (d < 10)
      return (char) ('0' + d);
    else
      return (char) ('a' + d - 10);
  }
}

