Skip to content
This repository
tree: 04a87a1211
Fetching contributors…

Cannot retrieve contributors at this time

file 347 lines (304 sloc) 13.224 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
/*
* Copyright 2006-2011 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.liftweb
package util

import java.net.{URLDecoder, URLEncoder}
import scala.collection.mutable.ListBuffer
import scala.xml._
import scala.collection.{Map}
import scala.collection.mutable.HashMap
import java.util.concurrent.atomic.AtomicLong
import common._

object HttpHelpers extends ListHelpers with StringHelpers

trait HttpHelpers {
  self: ListHelpers with StringHelpers =>
    
    /**
* The list of known suffixes used to split the URI into path parts and suffixes.
*/
    val knownSuffixes: Set[String] = Set("json","rss","atom","do","3dm",
                                         "3dmf","a","aab","aam","aas","abc","acgi","afl","ai","aif","aifc","aiff",
                                         "aim","aip","ani","aos","aps","arc","arj","art","asf","asm","asp","asx","au","avi","avs",
                                         "bcpio","bin","bm","bmp","boo","book","boz","bsh","bz","bz2","c","c++","cat","cc","ccad",
                                         "cco","cdf","cer","cha","chat","class","com","conf","cpio","cpp","cpt","crl","crt","csh",
                                         "css","cxx","dcr","deepv","def","der","dif","dir","dl","doc","dot","dp","drw","dump","dv",
                                         "dvi","dwf","dwg","dxf","dxr","el","elc","env","eps","es","etx","evy","exe","f","f77",
                                         "f90","fdf","fif","fli","flo","flx","fmf","for","fpx","frl","funk","g","g3","gif","gl","gsd",
                                         "gsm","gsp","gss","gtar","gz","gzip","h","hdf","help","hgl","hh","hlb","hlp","hpg","hpgl",
                                         "hqx","hta","htc","htm","html","htmls","htt","htx","ice","ico","idc","ief","iefs","iges","igs",
                                         "ima","imap","inf","ins","ip","isu","it","iv","ivr","ivy","jam","jav","java","jcm","jfif",
                                         "jfif-tbnl","jpe","jpeg","jpg","jps","js","jut","kar","ksh","la","lam","latex","lha","lhx",
                                         "list","lma","log","lsp","lst","lsx","ltx","lzh","lzx","m","m1v","m2a","m2v","m3u","man","map",
                                         "mar","mbd","mc$","mcd","mcf","mcp","me","mht","mhtml","mid","midi","mif","mime","mjf","mjpg",
                                         "mm","mme","mod","moov","mov","movie","mp2","mp3","mpa","mpc","mpe","mpeg","mpg","mpga","mpp",
                                         "mpt","mpv","mpx","mrc","ms","mv","my","mzz","nap","naplps","nc","ncm","nif","niff","nix",
                                         "nsc","nvd","o","oda","omc","omcd","omcr","p","p10","p12","p7a","p7c","p7m","p7r","p7s","part",
                                         "pas","pbm","pcl","pct","pcx","pdb","pdf","pfunk","pgm","pic","pict","pkg","pko","pl","plx","pm",
                                         "pm4","pm5","png","pnm","pot","pov","ppa","ppm","pps","ppt","ppz","pre","prt","ps","psd",
                                         "pvu","pwz","py","pyc","qcp","qd3","qd3d","qif","qt","qtc","qti","qtif","ra","ram","ras",
                                         "rast","rexx","rf","rgb","rm","rmi","rmm","rmp","rng","rnx","roff","rp","rpm","rt","rtf","rtx",
                                         "rv","s","s3m","saveme","sbk","scm","sdml","sdp","sdr","sea","set","sgm","sgml","sh","shar",
                                         "shtml","sid","sit","skd","skm","skp","skt","sl","smi","smil","snd","sol","spc","spl","spr",
                                         "sprite","src","ssi","ssm","sst","step","stl","stp","sv4cpio","sv4crc","svf","svr","swf","t",
                                         "talk","tar","tbk","tcl","tcsh","tex","texi","texinfo","text","tgz","tif","tiff","tr","tsi",
                                         "tsp","tsv","turbot","txt","uil","uni","unis","unv","uri","uris","ustar","uu","uue","vcd","vcs",
                                         "vda","vdo","vew","viv","vivo","vmd","vmf","voc","vos","vox","vqe","vqf","vql","vrml","vrt",
                                         "vsd","vst","vsw","w60","w61","w6w","wav","wb1","wbmp","web","wiz","wk1","wmf","wml","wmlc",
                                         "wmls","wmlsc","word","wp","wp5","wp6","wpd","wq1","wri","wrl","wrz","wsc","wsrc","wtk","x-png",
                                         "xbm","xdr","xgz","xif","xl","xla","xlb","xlc","xld","xlk","xll","xlm","xls","xlt","xlv","xlw",
                                         "xm","xml","xmz","xpix","xpm","xsr","xwd","xyz","z","zip","zoo","zsh")

  /**
* URL decode the string.
*
* This is a pass-through to Java's URL decode with UTF-8
*/
  def urlDecode(in : String) = URLDecoder.decode(in, "UTF-8")

  /**
* URL encode the string.
*
* This is a pass-through to Java's URL encode with UTF-8
*/
  def urlEncode(in : String) = URLEncoder.encode(in, "UTF-8")

  /**
* Take a list of name/value parse and turn them into a URL query string
*
* @param params the name/value pairs
* @return a valid query string
*/
  def paramsToUrlParams(params: List[(String, String)]): String = params.map {
    case (n, v) => urlEncode(n) + "=" + urlEncode(v)
  }.mkString("&")


  /**
* Append parameters to a URL
*
* @param url the url to append the params to
* @param params the parameters (name/value) to append to the URL
*
* @return the url with the parameters appended
*/
  def appendParams(url: String, params: Seq[(String, String)]): String = params.toList match {
    case Nil => url
    case xs if !url.contains("?") => url + "?" + paramsToUrlParams(xs)
    case xs => url + "&" + paramsToUrlParams(xs)
  }


  /**
* Given a map of HTTP properties, return true if the "Content-type"
* value in the map is either "text/html" or "application/xhtml+xml"
* @param in Map which may contain a key named Content-Type
* @return true if there is a pair ("Content-Type", "text/html") or
* ("Content-Type", "application/xhtml+xml")
*/
  def couldBeHtml(in: Map[String, String]): Boolean =
    in match {
      case null => true
      case n => {
        n.get("Content-Type") match {
          case Some(s) => { (s.toLowerCase == "text/html") ||
                           (s.toLowerCase == "application/xhtml+xml") }
          case None => true
        }
      }
    }

  /**
* Return true if the xml doesn't contain an <html> tag
*/
  def noHtmlTag(in: NodeSeq): Boolean = findElems(in)(_.label == "html").length != 1

  /**
* Transform a general Map to a nutable HashMap
*/
  def toHashMap[A,B](in : Map[A,B]) : HashMap[A,B] = {
    val ret = new HashMap[A,B];
    in.keysIterator.foreach { k => ret += Pair(k, in(k)) }
    ret
  }

  /**
* Ensure that all the appropriate fields are in the header.
*/
  def insureField(toInsure: List[(String, String)], headers: List[(String, String)]): List[(String, String)] = {
    def insureField_inner(toInsure : List[(String, String)], field : (String, String)): List[(String, String)] =
      toInsure.ciGet(field._1) match {
        case Full(_) => toInsure
        case _ => field :: toInsure
      }

    headers match {
      case Nil => toInsure
      case x :: xs => insureField(insureField_inner(toInsure, x), xs)
    }
  }

  /**
* Transform a pair (name: String, value: Any) to an unprefixed XML attribute name="value"
*/
  implicit def pairToUnprefixed(in: (String, Any)): MetaData = {
    val value: Option[NodeSeq] = in._2 match {
      case null => None
      case js: ToJsCmd => Some(Text(js.toJsCmd))
      case n: Node => Some(n)
      case n: NodeSeq => Some(n)
      case None => None
      case Some(n: Node) => Some(n)
      case Some(n: NodeSeq) => Some(n)
      case Empty => None
      case Full(n: Node) => Some(n)
      case Full(n: NodeSeq) => Some(n)
      case s => Some(Text(s.toString))
    }

    value.map(v => new UnprefixedAttribute(in._1, v, Null)) getOrElse Null
  }


  /**
* If the specified Elem has an attribute named 'id', return it, otherwise
* construct a new Elem with a randomly generated id attribute and return the pair
*
* @param in the element to test & add 'id' to
* @return the new element and the id
*/
  def findOrAddId(in: Elem): (Elem, String) = (in \ "@id").toList match {
    case Nil => {
      val id = nextFuncName
      (in % ("id" -> id), id)
    }
    case x :: xs => (in, x.text)
  }

  /**
* Within a NodeSeq, find the first elem and run it through
* the function. Return the resulting NodeSeq
*/
  def evalElemWithId(f: (String, Elem) => NodeSeq)(ns: NodeSeq): NodeSeq = {
    var found = false
    ns.flatMap {
      case e: Elem if !found => {
        found = true
        val (ne, id) = findOrAddId(e)
        f(id, ne)
      }
      case x => x
    }
  }
  
  /**
* Given a URL and a Lift function String, append the function
* even if the URL has query params and a #
*/
  def appendFuncToURL(url: String, funcStr: String): String =
  splitAtHash(url){to => to +
                   (if (to.indexOf("?") >= 0) "&" else "?") + funcStr}

  /**
* Split a String at the Hash sign, run the function
* on the non-# side and then append the hash side
*/
  def splitAtHash(str: String)(f: String => String): String =
  str.indexOf("#") match {
    case idx if idx < 0 => f(str)
    case idx => f(str.substring(0, idx)) + str.substring(idx)
  }

  

  private val serial = new AtomicLong(math.abs(Helpers.randomLong(Helpers.millis)) + 1000000L)

  /**
* Get a monotonically increasing number that's guaranteed to be unique for the
* current session
*/
  def nextNum = serial.incrementAndGet

  /**
* Find the elements of the specified NodeSeq that match
* the specified predicate and concatenate them into
* a resulting NodeSeq.
*
* @param nodes - the NodeSeq to search for elements matching the predicate
* @param f - the predicate to match elements with
* @return the NodeSeq resulting from concatenation of the matched elements.
*/
  def findElems(nodes: NodeSeq)(f: Elem => Boolean): NodeSeq = {
    val ret = new ListBuffer[Elem]
    def find(what: NodeSeq) {
      what.foreach {
        case Group(g) => find(g)
        case e: Elem =>
          if (f(e)) ret += e
        find(e.child)

        case n => find(n.child)
      }
    }
    find(nodes)

    ret.toList
  }

  /**
* Map the specified function over the elements of the
* specified NodeSeq and return the concatenated result.
* This is essentially a container-type-transforming flatMap operation.
*/
  def findInElems[T](nodes: NodeSeq)(f: Elem => Iterable[T]): List[T] = {
    val ret = new ListBuffer[T]

    def find(what: NodeSeq) {
      what.foreach {
        case Group(g) => find(g)
        case e: Elem =>
          ret ++= f(e)
        find(e.child)

        case n => find(n.child)
      }
    }

    find(nodes)

    ret.toList
  }

  /**
* Get a guaranteed unique field name
* (16 or 17 letters and numbers, starting with a letter)
*/
  def nextFuncName: String = nextFuncName(0)

  /**
* Get a guaranteed unique field name
* (16 or 17 letters and numbers, starting with a letter)
*/
  def nextFuncName(seed: Long): String = {
    val sb = new StringBuilder(24)
    sb.append('F')
    sb.append(nextNum + seed)
    // sb.append('_')
    sb.append(randomString(6))
    sb.toString
  }

  def findKids(in: NodeSeq, prefix: String, label: String): NodeSeq =
    in.filter(n => n.label == label && n.prefix == prefix).flatMap(_.child)

  def deepFindKids(in: NodeSeq, prefix: String, label: String): NodeSeq = {
    val ret: ListBuffer[Node] = new ListBuffer

    def doIt(in: NodeSeq) {
      in.foreach {
        case e: Elem if e.prefix == prefix && e.label == label =>
          e.child.foreach(ret.+=)
        case g: Group => doIt(g.nodes)
        case n => doIt(n.child)
      }
    }

    doIt(in)
    ret.toList
  }
}

/**
* TODO: Is this something that can be converted to a JavaScript Command
*/
trait ToJsCmd {
  def toJsCmd: String
}

object CheckNodeSeq {
  def unapply(in: Any): Option[NodeSeq] = in match {
    case Some(ns: NodeSeq) => Some(ns)
    case Full(ns: NodeSeq) => Some(ns)
    case Some(sq: Seq[_]) if sq.forall(_.isInstanceOf[Node])=> val ns: NodeSeq = sq.asInstanceOf[Seq[Node]]
    Some(ns)
    case Full(sq: Seq[_]) if sq.forall(_.isInstanceOf[Node])=> val ns: NodeSeq = sq.asInstanceOf[Seq[Node]]
    Some(ns)
    case ns: NodeSeq => Some(ns)
    case sq: Seq[_] if sq.forall(_.isInstanceOf[Node])=> val ns: NodeSeq = sq.asInstanceOf[Seq[Node]]
    Some(ns)
    case _ => None
  }
}
Something went wrong with that request. Please try again.